闪避和填充颜色后错误栏未对齐

时间:2018-06-24 17:41:52

标签: r ggplot2

我对以下正确绘制图形的数据集有疑问。数据如下:

weight  group   max    mean    min
1         1     2.3      3       4
1         2     2.3      3       4
1         3     2.5      3       4
2         1     1.5      2       3
2         2     1.5      2       3
2         3     1.5      2       3
3         1     4.2      5     5.4
3         2      4       5     5.6
3         3     3        5     5.8

这是我的代码:

library(reshape2)
library(ggplus)

sk4$mean <- as.numeric(as.character(sk4$mean))

sk4$min <- as.numeric(as.character(sk4$min))

sk4$max <- as.numeric(as.character(sk4$max))


sk4$group[sk4$group==1] <- "X"
sk4$group[sk4$group==2] <- "Y"
sk4$group[sk4$group==3] <- "Z"
# convert every column to numeric if character now
bold.text <- element_text(face = "bold")



# plot values seem a little wonky but the plot is coming together
ggplot(sk4, aes(x = as.factor(weight), y = mean, color = as.factor(group), width=.5)) +
  geom_bar( stat = "identity", size=0.5, position = "dodge", fill = "white") +

  geom_errorbar(aes(ymax = max, ymin = min), width=.2, position = "dodge") +
  theme_bw(base_size=10) + 
  theme(axis.text = bold.text) + 
  theme(axis.text=element_text(size=10),
        axis.title=element_text(size=12,face="bold")) +
  theme(axis.title = bold.text) +
  theme(legend.direction = "horizontal", legend.position = "bottom") +
  theme(plot.title = element_text(hjust = 0.5)) + 
  ggtitle("T")+ 
  xlab("A") + 
  ylab("B") +
  scale_x_discrete(breaks=c("1", "2", "3"),
                   labels=c("A", "B", "C"))+ 
  theme(legend.title = element_text(colour="black", size=10, face="bold"))+
  scale_colour_manual(name="legend", values=c("black", "#808080", "#844082")) + 
  scale_fill_manual("legend", values = c("X" = "black", "Y" = "orange", "Z" = "blue"))

enter image description here

我不确定为什么图例X,Y,Z没有填充为值“ values = c(” X“ =” black“,” Y“ =” orange“,” Z“ =” blue“ )”。此外,误差线的对齐方式未对齐。我寻找潜在的解决方案,但找不到任何东西。任何帮助将非常感激!

0 个答案:

没有答案