2017-06-21 62 views
0

我目前正在使用ggplot。 我有以下數據:如何使用ggplot對barplot進行排序

num <- c(5,5,5,5,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8) 
name <- c('4;1','4;1','4;1','4;1','4;2','4;2','4;2','3;3','4;2','3;3','4;2','3;3','4;2','4;3','3;4','4;3','3;4','4;3','3;4','4;3','3;4','4;3','3;4','4;3','3;4','4;3','4;4','4;4','4;4','4;4','4;4','4;4','4;4','4;4') 
x <- c(1.7,1.8,1.9,2.0,1.5,1.6,1.7,1.8,1.8,1.9,1.9,2.0,2.0,1.4,1.5,1.5,1.6,1.6,1.7,1.7,1.8,1.8,1.9,1.9,2.0,2.0,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0) 
min <- c(1.0,0.5,0.3,1.4,1.2,0.8,0.5,1.3,0.3,1.0,0.3,1.4,1.4,1.1,1.4,0.8,1.2,0.5,1.0,0.4,0.8,0.3,0.7,0.3,1.0,0.6,1.1,0.9,0.7,0.5,0.3,0.3,0.3,0.4) 
max <- c(1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,0.6) 
df <- data.frame(num,name,x,min,max) 

如可以看到的,該數據描述了配置的範圍。 我想要做的是這樣的事情: enter image description here

我試過了geom_bar選項,但沒有成功。有沒有人有一個想法,我可以如何使用ggplot這個數據和該類型的情節?

P.S. x軸應該是df$x,y軸應該是range(df$min, df$max)。顏色應該取決於df$name

df$num只是對數據進行分組。有時只有一個df$name一個df$num,有時有兩個df$name一個df$num

+1

我正在看你的問題。 [這裏](https://stackoverflow.com/questions/41882223/non-blocking-pthread-stop-or-why-does-stdatomic-flag-slow-down-my-code)你應該回報性能或[這裏](https://stackoverflow.com/questions/42271735/cast-raw-pointer-of-array-to-unique-ptr)答案明確地解決了您的問題和其他示例。請閱讀[爲什麼我應該接受答案?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。當你從中受益時,爲戰略目標做出貢獻很重要。乾杯。 – Masoud

回答

0

你的問題有點含糊,因爲它並不代表實際所需的圖形,但,這將是你的答案還是一個很好的出發點:

library(ggplot2) 
#set the whiskers to min/max as well 
#set the middle line to min or max 
#set the color to interaction of name and num columns 
p <- ggplot(df, aes(x = x, ymin = min, lower=min, 
     middle= min, upper=max, ymax = max , fill = interaction(name, num))) + 
geom_boxplot(stat="identity") + 
    coord_cartesian(ylim = c(0, 1.5)) 



library(ggpubr) 
ggpar(p, legend.title = "name.num") #just change the legend title 

這會給我們:

enter image description here

+0

非常好!非常感謝。是否有可能改變「勾號」大小?讓x和y軸得到0,1步? – Hymir

+0

@Hymir https://stackoverflow.com/questions/11335836/increase-number-of-axis-ticks – Masoud