我想用相同的图例集绘制几个不同的数据集,但并非所有数据集都包含所有图例标签,所以我想只绘制一个图例,并为每个图例标签使用相同的颜色。
我的数据看起来像这样
Sample Activity Location Value
brain A1 -99 0.000480219165072995
brain A1 -98 0.000310998665750027
brain A1 -97 0.00013269798404962
brain A1 -96 0.000414032362112828
brain A1 -95 0.000484106264682014
brain A1 -94 0.000277469810522874
brain A1 -93 -0.000312328089983588
brain A1 -92 -0.000326948367221977
brain A1 -91 -0.000566097491837788
brain A2 -99 0.023199362386866
brain A2 -98 0.0232008290610013
brain A2 -97 0.0235067519290527
brain A2 -96 0.0235475873183088
brain A2 -95 0.0237440466425034
brain A2 -94 0.0240249966894288
brain A2 -93 0.0245502842927103
brain A2 -92 0.0244587160446747
brain A2 -91 0.0252699000904297
所以我想为Activity绘制两条线,为A1绘制一种颜色,为A2等绘制另一种颜色。
大约有8个不同的活动和许多地点。
如何为每个活动手动设置颜色?例如,A1将始终为红色,A2为黑色,A3为蓝色等。?
ggplot(data=df,aes(x=Location,y=Value,group=Activity))+geom_line(aes(colour=Activity),size=1.5)+theme_bw()
答案 0 :(得分:1)
只需遵循@MrFlick's comment中的建议(下面的代码)
df <- structure(list(Sample = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "brain", class = "factor"),
Activity = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A1", "A2"
), class = "factor"), Location = c(-99L, -98L, -97L, -96L,
-95L, -94L, -93L, -92L, -91L, -99L, -98L, -97L, -96L, -95L,
-94L, -93L, -92L, -91L), Value = c(0.000480219165072995,
0.000310998665750027, 0.00013269798404962, 0.000414032362112828,
0.000484106264682014, 0.000277469810522874, -0.000312328089983588,
-0.000326948367221977, -0.000566097491837788, 0.023199362386866,
0.0232008290610013, 0.0235067519290527, 0.0235475873183088,
0.0237440466425034, 0.0240249966894288, 0.0245502842927103,
0.0244587160446747, 0.0252699000904297)), .Names = c("Sample",
"Activity", "Location", "Value"), class = "data.frame", row.names = c(NA,
-18L))
# install.packages("ggplot2", dependencies = TRUE)
library(ggplot2)
p <- ggplot(data=df,aes(x=Location,y=Value,group=Activity))+geom_line(aes(colour=Activity),size=1.5)+theme_bw()
p + scale_color_manual(values=c("red", "blue")) # add more …