2017-08-24 42 views
1

我想在顯示0值的兩個線圖中有X個符號。ggplot:在線圖中的特定點創建X符號

我有數據如下:

data <- read.table(text = " 
Clients Throughput Systems ErrorBar 
250 2494 Za  72.986 
500 2491 Za  104.854 
750 401 Za  130.034 
1000 0 Za  0 
1250 0 Za  0 
250 2501 ZCT 58.987 
500 2499 ZCT 89.927 
750 2380 ZCT 130.475 
1000 2321 ZCT 139.450 
1250 2296 ZCT 142.580", header = TRUE) 

繪製線圖如下腳本:

plot1 <- ggplot(data=data , aes(x=Clients, y=Throughput, group=Systems, colour = Systems, shape=Systems)) + 
    geom_errorbar(aes(ymin=Throughput-Error, ymax=Throughput+Error), width=20.5, colour="black") + 
    geom_line(size=1.2) + 
    geom_point(size=2.3) 

plot1 <- plot1 + scale_y_continuous(breaks= seq(0,3000,500), limits = c(0,3000)) + 
    labs(x="Number of clients") + 
    scale_x_continuous(breaks = c(250,500,750,1000,1250)) + 
    labs(y="Throughput (abds/sec)") 

plot1 <- plot1 + scale_colour_manual(values=c("#00ff00","#0000ff")) 

plot1 <- plot1 + theme_bw() + 
    theme(legend.position="bottom") + 
    labs(fill="", colour=" ", shape=" ") + 
    theme(text = element_text(size=18)) + 
    guides(fill = guide_legend(keywidth = 0.8, keyheight = 0.01)) 

plot1 

任何幫助嗎?

回答

1

可以使用annotate添加X符號:

data <- read.table(text = " 
Clients Throughput Systems Error 
250 2494 Za  72.986 
500 2491 Za  104.854 
750 401 Za  130.034 
1000 0 Za  0 
1250 0 Za  0 
250 2501 ZCT 58.987 
500 2499 ZCT 89.927 
750 2380 ZCT 130.475 
1000 2321 ZCT 139.450 
1250 2296 ZCT 142.580", header = TRUE) 

plot1 <- ggplot(data=data , aes(x=Clients, y=Throughput, group=Systems, colour = Systems, shape=Systems)) + 
    geom_errorbar(aes(ymin=Throughput-Error, ymax=Throughput+Error), width=20.5, colour="black") + 
    geom_line(size=1.2) + 
    geom_point(size=2.3) 

plot1 <- plot1 + scale_y_continuous(breaks= seq(0,3000,500), limits = c(0,3000)) + 
    labs(x="Number of clients") + 
    scale_x_continuous(breaks = c(250,500,750,1000,1250)) + 
    labs(y="Throughput (abds/sec)") 

plot1 <- plot1 + scale_colour_manual(values=c("#00ff00","#0000ff")) 

plot1 <- plot1 + theme_bw() + 
    theme(legend.position="bottom") + 
    labs(fill="", colour=" ", shape=" ") + 
    theme(text = element_text(size=18)) + 
    guides(fill = guide_legend(keywidth = 0.8, keyheight = 0.01))+ 
    annotate("Text", x=data$Clients[data$Throughput==0], 
        y=data$Throughput[data$Throughput==0], label="X", size=6) 

plot1 

enter image description here

+0

是的,這就是我一直在尋找。非常感謝你@Marco Sandri –

+0

我剛剛遇到另一個問題,當我在750,1000和1250中有0時,它只在1000和1250中顯示X符號。如何在750數據點中添加X? –

+0

謝謝@Marco Sandri,它的工作原理。 –