Я все еще очень новичок в использовании GGPLOT, но я создал следующую графику, в которой я хотел бы поменять местами синий и красный цвета. Должно быть достаточно просто, но я не могу понять это.
df <- structure(list(Sex = c("M", "M", "M", "M", "M", "M", "M", "W",
"W", "W", "W", "W", "W", "W"), age_cat = structure(c(1L, 2L,
3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("<40",
"41-50", "51-60", "61-70", "71-80", "81-90", "90+"), class = "factor"),
DD = c(42L, 88L, 289L, 558L, 527L, 174L, 22L, 27L, 36L, 206L,
347L, 321L, 160L, 29L), pop = c(36642L, 16327L, 20232L, 18068L,
14025L, 5555L, 1293L, 35887L, 16444L, 20178L, 17965L, 14437L,
7150L, 2300L), proportion = c(0.114622564270509, 0.538984504195504,
1.428430209569, 3.08833296435687, 3.75757575757576, 3.13231323132313,
1.7014694508894, 0.0752361579402012, 0.218924835806373, 1.02091386658737,
1.9315335374339, 2.22345362609961, 2.23776223776224, 1.26086956521739
), lower = c(0.082621962613957, 0.432499099174075, 1.26946115577044,
2.8408823120445, 3.44891013496043, 2.69000601596679, 1.06929480146528,
0.0495867729368698, 0.153377923598767, 0.886828947142727,
1.73530361873497, 1.98914206124244, 1.90751612365318, 0.846006018532107
), upper = c(0.154905173671422, 0.663628389658291, 1.6015714811397,
3.35102939015014, 4.08561035940466, 3.6247050150149, 2.56476800800746,
0.10944592449968, 0.302956684874059, 1.16937842460687, 2.14353263545661,
2.47728262910991, 2.60770261266057, 1.80583393021473)), row.names = c(NA,
-14L), class = "data.frame")
Ниже приведен сценарий, который я использовал, в котором я получаю (sex = M) красным и (sex = W) синим.
ggplot(data = prevalence2021GGPLOT,
aes(x = age_cat, y = proportion, color = Sex))+
geom_point()+
labs(title = "Prevalence 2021", y = "Prevalence (%)", x = "Age category") +
geom_errorbar(aes(ymin=(lower),
ymax=(upper)), width=.2) +
theme_bw()+
geom_line(aes(group = unlist(Sex)))
Как сделать секс = M синим, а секс = W красным??
Это работает. Мне нужно указать на df, чтобы использовать ваш образец df. Отредактировал его по предложению.
ggplot(data = df,
aes(x = age_cat, y = proportion, color = Sex))+
geom_point()+
labs(title = "Prevalence 2021", y = "Prevalence (%)", x = "Age category") +
geom_errorbar(aes(ymin=(lower),
ymax=(upper)), width=.2) +
theme_bw()+
geom_line(aes(group = unlist(Sex))) +
ggplot(data = df,
aes(x = age_cat, y = proportion, color = Sex))+
geom_point()+
labs(title = "Prevalence 2021", y = "Prevalence (%)", x = "Age category") +
geom_errorbar(aes(ymin=(lower),
ymax=(upper)), width=.2) +
theme_bw()+
geom_line(aes(group = unlist(Sex))) +
scale_color_manual(values=c(M = "darkblue", W = "darkred"))
Чтобы убедиться, что цветам присвоены правильные значения, используйте именованный вектор c(M = "darkblue", W = "darkred")
Вы можете использовать scale_color_manual
, чтобы вручную изменить цвета в ggplot2. Первый цвет соответствует первой модальности вашей переменной заполнения.
ggplot(data = df,
aes(x = age_cat, y = proportion, color = Sex))+ geom_point()+
labs(title = "Prevalence 2021", y = "Prevalence (%)", x = "Age category") +
geom_errorbar(aes(ymin=(lower),
ymax=(upper)), width=.2) +
theme_bw()+
geom_line(aes(group = unlist(Sex)))+
scale_color_manual(values=c("blue", "red"))
В результате :
вы можете найти документацию по графике ggplot2 на R здесь:
Это может быть полезно stackoverflow.com/a/5171329/1315767