Student
我的代码是
# Load data
d <– structure(list(author = structure(c(1L, 2L, 4L, 3L, 5L, 6L, 8L,11L, 13L, 12L, 10L,
9L, 7L), .Label = c("Bahr et al", "Fuller et al","Garbossa et al",
"Gokhale et al", "Iuchi et al", "Lee et al","Lee Y et all", "Merrel et al",
"Newton et al", "Rossetti et al", "Usery et al", "Wychowski et al",
"Zachenhofer et al"), class = "factor"),nAE = c(22L, 34L, 158L, 90L, 70L,
41L, 48L, 32L, 73L, 23L,25L, 13L, 46L), AE = c(3L, 1L, 7L, 1L, 3L, 10L, 3L,
6L, 3L,5L, 4L, 6L, 5L), SAE = c(0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,2L, 0L, 2L,
0L, 0L)), .Names = c("author", "nAE", "AE", "SAE"), class = "data.frame",
row.names = c(NA, -13L))
当我运行附加的代码时,R给我这个错误代码:“提供给连续比例的离散值”。我无法弄清楚为什么翻转的y轴没有在代码中写入标记(“作者”)。
你能搞清楚吗?
提前致谢, 下进行。
答案 0 :(得分:1)
问题是author
不是连续,而是离散。因此,在下面的更新代码中使用scale_x_discrete()
,
d %>%
gather(key, value, -author) %>%
ggplot(aes(author, value, fill = key)) +
geom_col(alpha=0.9) +
coord_flip() +
scale_x_discrete(name="Author") + # Here! scale_x_discrete()
scale_y_continuous(name="Number of observations", limits=c(0, 170),
seq(0,170,by=10)) +
theme_grey() +
theme(legend.position = "top") +
scale_fill_manual(labels = categories, values = cols)
如果您使用labs()
,可能会更清晰
d %>%
gather(key, value, -author) %>%
ggplot(aes(author, value, fill = key)) +
geom_col(alpha=0.9) +
coord_flip() +
theme(legend.position = "top") +
scale_fill_manual(labels = categories, values = cols) +
labs(y = "Number of observations", x = "Author")
然后再次如果您要指定limits()
和breaks
,则可能不值得。无论如何,我希望这是有帮助的。