所以,我有.csv格式的数据集,附加here,我想在ggplot2中制作以下图表。
df1 <- read.csv("obesity.csv")
ggplot(df1, aes(x = Levels, y = OR, color = col)) +
geom_point(size = 3) +
geom_errorbar(aes(ymax = UCI, ymin = LCI), width = 0.3) +
coord_flip() +
facet_grid(name ~ ., scales = "free_y", space = "free_y", switch = "y") +
geom_hline(yintercept = 1, size=1, col="brown") +
scale_x_discrete(position = "top") +
theme_bw() +
scale_color_manual(guide = FALSE, values = c("red", "blue", "black", "green", "orange",
"chocolate", "brown", "grey", "tomato", "navyblue", "purple1", "orchid4"))
正如您所看到的,颜色的顺序与我选择的完全不同。 但是,如果我自己在R中创建数据,则没有问题。
df2 <- data.frame(
Levels = as.factor(c("Africa", "Europe", "Latin America", "Middle East", "High",
"Upper middle", "Lower middle", "Secondary", "Post-Secondary", ">= 9 years", "Male",
"> 60 min/day")),
OR = c(0.72, 3.17, 0.51, 0.51, 0.38, 0.94, 1.04, 2.22, 3.49, 2.24, 1.9, 0.44),
LCI = c(0.23, 0.68, 0.09, 0.2, 0.09, 0.29, 0.49, 0.72, 1.33, 1.14, 0.93, 0.19),
UCI = c(2.28,9.5,2.92,1.32,1.62,3.07,2.22,6.77,9.16,4.39,3.87,1.03),
name = as.factor(c("Region", "Region", "Region", "Region", "Income", "Income",
"Income", "Education", "Education", "Age", "Sex", "PAL")),
col = paste("col",1:12, sep=""))
现在,我正在尝试了解2之间的区别,当我使用csv格式的数据集时,如何以我想要的方式设置颜色。
编辑:我将df2中的变量更改为因子,而不是字符。