我正在尝试为每个x重新排序堆叠的geom_bar,但没有成功。我想要实现的是一个曲线图,其中对于每个x的值,y值从小到大排序。

然而,问题似乎是ggplot威胁y是离散值,而不是连续值,因此我无法更改y轴的断点和标签。我尝试过使用scale_x_discrete,但没有成功。
library(tidyverse)
df <- data.frame(q= c(rep("2011", 3), rep("2012", 3)),
typ = rep(c("A", "B", "C"), 2),
val = c(7,2,1,2,3,4), stringsAsFactors = F) %>% as_tibble()
ggplot(df) + geom_col(mapping = aes(x = q, y = reorder(val, val), fill = typ))
ggplot(df) + geom_col(mapping = aes(x = q, y = reorder(val, val), fill = typ)) + scale_y_continuous()
Error: Discrete value supplied to continuous scale下面的代码根本不会改变我的中断。
ggplot(df) + geom_col(mapping = aes(x = q, y = reorder(val, val), fill = typ)) + scale_y_discrete(breaks = 1:10)发布于 2019-09-05 17:57:39
通过@kath中的help,我设法解决了这个问题
library(tidyverse)
df <- data.frame(q= c(rep("2011", 3), rep("2012", 3)),
typ = rep(c("A", "B", "C"), 2),
val = c(7,2,1,2,3,4), stringsAsFactors = F) %>% as_tibble()
bars <- map(unique(df$q)
, ~geom_bar(stat = "identity", position = "stack"
, data = df %>% filter(q == .x)))
df %>%
ggplot(aes(x = q, y = val, fill = reorder(typ,val))) +
bars +
guides(fill=guide_legend("ordering")) +
scale_y_continuous(breaks = 1:10, limits = c(0, 10))https://stackoverflow.com/questions/57802415
复制相似问题