我有这个data.frame:
df = data.frame(x = c(1,1,2,2,3,3), factor = c("crb","crb","ctx","ctx","bsl","bsl"), factor.level = c("pat","mat","pat","mat","pat","mat"), factor.level.color = c("blue","red","blue","red","blue","red"), ymin = c(0.031,0.152,0.071,0.026,0.051,0.032), lower = c(0.119,0.522,0.415,0.185,0.287,0.285), middle = c(0.298,0.701,0.615,0.384,0.500,0.499), upper = c(0.477,0.880,0.814,0.584,0.714,0.714), ymax = c(0.848,0.870,0.874,0.929,0.967,0.950),stringsAsFactors=F)
并且我想在geom_boxplot
中绘制它,以便df$x
定义x轴位置,df$ymin
,df$lower
,{{1} },df$middle
,df$upper
定义框,df$ymax
定义x轴刻度标签,df $ factor.level.color定义框的颜色,df$factor
应出现在图例中(唯一值)。应在同一x轴位置绘制一对方框。因此,需要定义透明度因子
这是我希望它看起来像的即兴版本:
(显然缺少x和y轴刻度值)
我认为这段代码是我需要的:
df$factor.level
但结果有点混乱:
我想水平需要以某种方式定义,但我不知道该怎么做。
答案 0 :(得分:1)
在aes(x = factor(x))
ggplot(...)
pl = ggplot(data = df, aes(x = factor(x))) +
geom_boxplot(aes(lower = lower,upper = upper, middle = middle,
ymin = ymin, ymax = ymax,
color = factor.level.color, fill = factor.level.color),
position = position_dodge(width = 0),
width = 0.5, alpha = 0.5, stat = "identity") +
scale_fill_identity("factor level", guide="legend", labels = df$factor.level) +
scale_color_identity("factor level", guide = "legend", labels = df$factor.level) +
scale_x_discrete(breaks = seq(1,length(unique(df$factor)),1),
labels = unique(df$factor),
limits = seq(1,length(unique(df$factor)),1)) +
labs(x = "factor",y = "fraction") +
theme(axis.title.x = element_text(size = rel(0.8)), axis.title.y = element_text(size = rel(0.8)))