我有一些数据(请参阅下文),我想用线(mean
)和upper
和lower
界线绘制。我已经实现了这一点(请参见下面的代码),但想分别更改上下行的颜色。
请考虑以下数据:
data <- structure(list(Row = c (1L, 2L, 3L, 4L, 7L, 8L, 9L, 10L, 13L, 14L, 15L, 16L, 19L, 20L, 21L, 22L, 25L, 26L, 27L, 28L, 31L, 32L, 33L, 34L, 37L, 38L, 39L, 40L, 43L, 44L, 45L, 46L, 49L, 50L, 51L, 52L, 55L, 56L, 57L, 58L, 61L, 62L, 63L, 64L),
mean = c(0.02298658,0.05299030,0.08299402,0.11299774,0.03075102,0.06075596,0.09076089,0.12076583,0.04106720,0.07107372,0.10108024,0.13108676,0.05469522,0.08470377,0.11471233,0.14472088,0.07255268,0.10256381,0.13257493,0.16258606,0.09569158,0.12570589,0.15572019,0.18573450,0.12522569,0.15524379,0.18526188,0.21527998,0.16218708,0.19220950,0.22223192,0.25225435,0.20730452,0.23733159,0.26735866,0.29738574,0.26073142,0.29076310,0.32079479,0.35082648,0.32180096,0.35183675,0.38187253,0.41190832),
lower = c(0.02001936,0.05002340,0.08002744,0.11003149,0.02637643,0.05638214,0.08638786,0.11639357,0.03428711,0.06429811,0.09430911,0.12432012,0.04347790,0.07348976,0.10350163,0.13351350,0.05491805,0.08492779,0.11493754,0.14494730,0.06842579,0.09844165,0.12845752,0.15847338,0.08541780,0.11543727,0.14545674,0.17547622,0.10708009,0.13711512,0.16715016,0.19718520,0.13200609,0.16202424,0.19204240,0.22206056,0.16270517,0.19274097,0.22277678,0.25281259,0.19905517,0.22907373,0.25909230,0.28911087),
upper = c(0.02629616,0.05629639,0.08629662,0.11629685,0.03546341,0.06546877,0.09547412,0.12547948,0.04879853,0.07880438,0.10881024,0.13881610,0.06729005,0.09729357,0.12729748,0.15730640,0.09335719,0.12336202,0.15336686,0.18337170,0.12862716,0.15863947,0.18864678,0.21865291,0.17360485,0.20361856,0.23363227,0.26364598,0.23140493,0.26145163,0.29149834,0.32152234,0.30024572,0.33026912,0.36029252,0.39031592,0.37938950,0.40942921,0.43946892,0.46950864,0.46744402,0.49750282,0.52756161,0.55762041),
x = c( 0L, 0L, 0L, 0L,5L, 5L, 5L, 5L,10L, 10L, 10L, 10L,15L, 15L, 15L, 15L,20L, 20L, 20L, 20L,25L, 25L, 25L, 25L,30L, 30L, 30L, 30L,35L, 35L, 35L, 35L,40L, 40L, 40L, 40L,45L, 45L, 45L, 45L,50L, 50L, 50L, 50L),
cat = c("A", "B", "C", "D","A", "B", "C", "D", "A", "B", "C", "D","A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D","A", "B", "C", "D","A", "B", "C", "D","A", "B", "C", "D","A", "B", "C", "D")),
row.names = c(NA, -44L),
class = c("data.table", "data.frame"))
当我使用
生成图时ggplot(data = data, aes(y = mean, ymin = lower, ymax = upper, x = x, fill = cat, alpha = cat)) +
scale_fill_manual("category", values = c("#11CC66","#2277FF", "#AFAFAF", "#FF2277"),
labels = c("A", "B", "C", "D")) +
theme_classic() +
theme(legend.position = c(.2,.7)) +
geom_ribbon() + # confidence intervals
geom_line(data = data, aes(y = mean)) + # mean
scale_y_continuous("Predicted probability", labels = scales::percent) + # axis
xlab("X") +
labs(title = "Title")+
scale_alpha_manual(name = "Category", values = c(.3, .3, .3, .3)) +
geom_line(data = data, aes(y = mean), size = 2, linetype = 1) + # all means
geom_line(data = data[data$cat == "A", ], aes(y = lower), size = .2, linetype = 3) + # A lower
geom_line(data = data[data$cat == "A", ], aes(y = upper), size = .2, linetype = 3) + # A upper
geom_line(data = data[data$cat == "C", ], aes(y = lower), size = 1, linetype = 1) + # C lower
geom_line(data = data[data$cat == "C", ], aes(y = upper), size = 1, linetype = 1) + # C upper
geom_line(data = data[data$cat == "D", ], aes(y = lower), size = .2, linetype = 5) + # D lower
geom_line(data = data[data$cat == "D", ], aes(y = upper), size = .2, linetype = 5) # D upper
我想分别更改upper
和lower
行的颜色,即A lower
,A upper
,B lower
等(请参见注释行) )设为任意的RBG值(alpha = .6
),例如#CCCC00
,#00FC00
,#11CC00
。
答案 0 :(得分:1)
您几乎在那里,只需为每行添加一个唯一的color=#xxxxxx
。
例如
...
geom_line(data = data[data$cat == "C", ], aes(y = lower), size = 1, linetype = 1, color="#8c8c8c) + # C lower
geom_line(data = data[data$cat == "C", ], aes(y = upper), size = 1, linetype = 1, color="#595959") + # C upper
...