谁能告诉我这个脚本出了什么问题?
我需要2条水平,黑色,虚线,但是我得到了2条红色连续线。
尽管正在使用theme_bw,但我也无法将绘图边距的颜色更改为黑色,并且根据需要,箱线图的填充也不是灰色的。
dat1 <- data.frame (xvar = rep(c("A", "B"), each=10),
yvar = 1:20 + rnorm(20,sd=3))
ggplot(dat1, aes(x=xvar, y=yvar)) +
theme_bw()+
geom_boxplot(fill=grey)+
geom_hline(aes(yintercept=40, color="black", linetype="dashed"))+
geom_hline(aes(yintercept=33.84, color="black", linetype="dashed"))+
scale_x_discrete(name="") +
scale_y_continuous(name="temperature (°C)")+
opts(
panel.grid.major = theme_line(size = 0.5, colour = NA),
panel.background = theme_rect(colour = NA),
axis.title.y = theme_text(angle=90,face="bold", colour="black", size=14),
axis.text.y = theme_text(face="bold",angle=0, size=14,colour="black"),
axis.title.x = theme_text(face="bold", colour="black", size=14),
axis.text.x = theme_text( size=14,vjust=1.2, colour=NA))
多谢!
最佳答案
关于黑色虚线,您应该在aes()之外定义它。请尝试以下代码:
geom_hline(aes(yintercept=40), color="black", linetype="dashed")
关于箱形图,您应该将代码更正为以下代码:
geom_boxplot(fill="gray")
最后,要获得黑色边距,请注意,您正在设置边距以使opts(...,panel.background = theme_rect(colour = NA),...)中具有NA颜色。要解决此问题,请尝试以下操作:
panel.background = theme_rect(colour = "black")
希望我的评论有帮助。
关于r - 控制ggplot2中水平线的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11287581/