如何在geom_point中显示不同的颜色?
How to display different colors in geom_point?
这是我的数据框:
df = structure(list(qCountry = c("AT", "DE", "ES", "FI", "FR", "GR",
"HU", "IR", "IT", "LV", "NL", "POL", "PT", "RO", "SWE", "EU"),
Mean = c(1.34199395770393, 1.37664132688321, 1.29144095341278,
1.42088404868674, 1.45019920318725, 1.29786200194363, 1.24528301886792,
1.26937046004843, 1.38345864661654, 1.39706780696396, 1.38751714677641,
1.30804248861912, 1.28609062170706, 1.2320819112628, 1.32588699080158,
1.33425470556542), Sd = c(0.474520963779525, 0.484711259139164,
0.454549282145671, 0.493859200850428, 0.497678958439859,
0.45742966912063, 0.430377930416405, 0.443767080631815, 0.48638085640142,
0.489439780963027, 0.487350499450418, 0.461801022550429,
0.452051373271304, 0.422281105345888, 0.468859354590332,
0.467003889139754)), row.names = c(NA, -16L), class = c("tbl_df",
"tbl", "data.frame"))
然后我绘制如下:
c5_1 %>%
ggplot(aes(x = reorder(qCountry,-Mean), y = Mean, group = qCountry)) +
geom_point(size = 3.5) +
geom_errorbar(aes(ymin=Mean-Sd, ymax=Mean+Sd)) +
labs(title = "Some Title", subtitle = "Some subtitle",x = "", y="") +
scale_y_continuous(breaks = c(1, 2),
labels = c("Yes", "No"), limits = c(0.8, 2)) +
coord_flip() +
theme_classic()
这个问题是我希望“EU”只有显示为红色,而其余的保持黑色。
有人可以帮我吗?
谢谢!
您可以使用颜色名称创建一个新列并调用 scale_color_identity()
:
df %>%
mutate(color_plot = ifelse(qCountry == "EU", "red", "black")) %>%
ggplot(aes(x = reorder(qCountry,-Mean), y = Mean, group = qCountry,
color = color_plot)) +
geom_point(size = 3.5) +
geom_errorbar(aes(ymin=Mean-Sd, ymax=Mean+Sd)) +
labs(title = "Increasing 1% of your personal tax bill for financing financial support to other EU member states", subtitle = "Natural Disaster",x = "", y="") +
scale_y_continuous(breaks = c(1, 2),
labels = c("Yes", "No"), limits = c(0.8, 2)) +
coord_flip() +
scale_color_identity() +
theme_classic()
输出为:
这是我的数据框:
df = structure(list(qCountry = c("AT", "DE", "ES", "FI", "FR", "GR",
"HU", "IR", "IT", "LV", "NL", "POL", "PT", "RO", "SWE", "EU"),
Mean = c(1.34199395770393, 1.37664132688321, 1.29144095341278,
1.42088404868674, 1.45019920318725, 1.29786200194363, 1.24528301886792,
1.26937046004843, 1.38345864661654, 1.39706780696396, 1.38751714677641,
1.30804248861912, 1.28609062170706, 1.2320819112628, 1.32588699080158,
1.33425470556542), Sd = c(0.474520963779525, 0.484711259139164,
0.454549282145671, 0.493859200850428, 0.497678958439859,
0.45742966912063, 0.430377930416405, 0.443767080631815, 0.48638085640142,
0.489439780963027, 0.487350499450418, 0.461801022550429,
0.452051373271304, 0.422281105345888, 0.468859354590332,
0.467003889139754)), row.names = c(NA, -16L), class = c("tbl_df",
"tbl", "data.frame"))
然后我绘制如下:
c5_1 %>%
ggplot(aes(x = reorder(qCountry,-Mean), y = Mean, group = qCountry)) +
geom_point(size = 3.5) +
geom_errorbar(aes(ymin=Mean-Sd, ymax=Mean+Sd)) +
labs(title = "Some Title", subtitle = "Some subtitle",x = "", y="") +
scale_y_continuous(breaks = c(1, 2),
labels = c("Yes", "No"), limits = c(0.8, 2)) +
coord_flip() +
theme_classic()
这个问题是我希望“EU”只有显示为红色,而其余的保持黑色。
有人可以帮我吗?
谢谢!
您可以使用颜色名称创建一个新列并调用 scale_color_identity()
:
df %>%
mutate(color_plot = ifelse(qCountry == "EU", "red", "black")) %>%
ggplot(aes(x = reorder(qCountry,-Mean), y = Mean, group = qCountry,
color = color_plot)) +
geom_point(size = 3.5) +
geom_errorbar(aes(ymin=Mean-Sd, ymax=Mean+Sd)) +
labs(title = "Increasing 1% of your personal tax bill for financing financial support to other EU member states", subtitle = "Natural Disaster",x = "", y="") +
scale_y_continuous(breaks = c(1, 2),
labels = c("Yes", "No"), limits = c(0.8, 2)) +
coord_flip() +
scale_color_identity() +
theme_classic()
输出为: