r - ggplot2:使线上的点颜色比线颜色深

标签 r ggplot2

我想让图表上的每个点都与直线颜色不同。这是示例数据。

df <- structure(list(yrmonth = structure(c(17167, 17167, 17167, 17198, 
17198, 17198, 17226, 17226, 17226, 17257, 17257, 17257), class = "Date"), 
    index = structure(c(2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 
    1L, 3L), .Label = c("E-W", "N-S", "OS"), class = "factor"), 
    N = c(2, 2, 1, 2, 2, 1, 2, 2, 1, 2, 2, 1), data = c(129, 
    141, 27, 150.5, 209, 87, 247.5, 243, 188, 223, 226.5, 170
    )), .Names = c("yrmonth", "index", "N", "data"), row.names = 31:42, class = "data.frame")

这是我的情节代码。

df$yrmonth <- lubridate::ymd(df$yrmonth)

ggplot(df, aes(x=yrmonth,y=data,colour=factor(index), group=index)) + 
  geom_line(size=.4) + 
  geom_point(size=1)

我希望绿点是深绿色,橙色点是深橙色等等。

最佳答案

您可以使用填充点标记(形状 21 到 25),这将允许您将点的填充颜色与线的颜色分开设置。在下面的代码中,我对点和线使用相同的色调(hcl 函数的 h 参数),但亮度较低(l 参数到 hcl) 的点,以便它们比线条更暗。我还增加了线条和点的大小,以便更容易看出差异。

ggplot(df, aes(x=yrmonth,y=data)) + 
  geom_line(size=1, aes(colour=factor(index))) + 
  geom_point(size=3, aes(fill=factor(index)), shape=21, colour="#FFFFFF00") +
  scale_colour_manual(values=hcl(seq(15,375,length=4)[1:3], 100, 70)) +
  scale_fill_manual(values=hcl(seq(15,375,length=4)[1:3], 100, 40)) +
  theme_classic() +
  labs(colour="Index", fill="Index")

enter image description here

关于r - ggplot2:使线上的点颜色比线颜色深,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44597946/

相关文章:

r - R语言获取字符 "\n 0.28\n\n "的数字

r - 在ggplot2中着色箱线图异常点?

r - 如何在 R ggplot 中设置用于不同组的形状?

r - 从ggplot条形图中排除零值?

r - 如何在ggplot2中修改箱线图的晶须?

r - 如何从 ggplot 对象中提取填充颜色?

r - 带有 ggplot 的错误栏 - 出现错误

r - lme4::glmer.nb 函数根据我运行模型的顺序产生 "Error in family$family : $ operator not defined for this S4 class"

r - 为什么用数字键设置子集不适用于 data.table

r - R中的加权数字总和