问题描述
我想使图形上的每个点的颜色都与线的颜色不同.这是示例数据.
I would like to make each point on the graph a different color from the line. Here is sample data.
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)
我希望绿色的点是深绿色,橙色的点是深橙色,依此类推.
I would like the green dots to be a darker green, the orange dots to be a darker orange and so forth.
推荐答案
您可以使用填充点标记(形状21到25),这将使您可以将点的填充颜色与线条的颜色分开设置.在下面的代码中,我对点和线使用相同的色调(对hcl
函数的h
参数),对点使用较低的亮度(对hcl
的l
参数),以便它们会比线条更暗.我还增加了线和点的大小,以便于查看差异.
You could use a filled point marker (shapes 21 through 25), which would allow you to set the fill colors for the points separately from the colors of the lines. In the code below, I use the same hues (the h
argument to the hcl
function) for the points and lines, but a lower luminance (the l
argument to hcl
) for the points so that they will be darker than the lines. I've also increased the line and point sizes to make it easier to see the difference.
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")
这篇关于ggplot2:使线上的点比线的颜色更深的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!