【问题标题】:How to ranged barplot with ggplot如何使用 ggplot 绘制范围条形图
【发布时间】:2017-06-21 11:14:51
【问题描述】:

我目前正在努力使用 ggplot。 我有以下数据:

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)

可以看出,数据描述了配置范围。 我想做的是这样的:

我尝试了 geom_bar 选项,但完全没有成功。有谁知道,我如何将 ggplot 用于这些数据和那种类型的绘图?

P.S. x 轴应为df$x,y 轴应为range(df$min, df$max)。颜色应该取决于df$name

df$num 只是对数据进行分组。有时一个df$num只有一个df$name,有时一个df$num有两个df$name

【问题讨论】:

  • 我正在查看您的问题。 here您应该报告性能或here答案清楚地解决了您的问题和其他示例。请阅读Why I should accept an answer?。当您从中受益时,为 SO 做出贡献很重要。干杯。

标签: r ggplot2


【解决方案1】:

您的问题有点含糊,因为它并不代表实际所需的图表,但这将是您的答案或一个好的起点:

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

这会给我们:

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-18
相关资源
最近更新 更多