r - 控制ggplot2中水平线的颜色

标签 r ggplot2

谁能告诉我这个脚本出了什么问题?
我需要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/

相关文章:

r - 如何将 "vertex sequence"的列表转换为向量

r - `range` 和 `pips` 参数如何在shinyWidgets `noUiSliderInput()` 中工作?

r - ggplot2 : Adding two errorbars to each point in scatterplot

r - ggplot2:geom_line 的大小和位置

r - R 匹配字符串模式中矩阵运算的向量化

r - 如何使用 dplyr 函数在数据表中添加新列?

R复制行并用重复的id除以值

r - ggplot2 热图,图表之间具有固定比例的颜色条

用另一个默认主题中的元素替换一个 ggplot 默认主题元素

r - qplot 堆栈条形图