Stacked ggplot2 geom_area 重新运行一个空图
Stacked ggplot2 geom_area reruns an empty graph
我正在尝试 geom_area 生成堆积面积图,但它生成的是入口图。这是一个例子
library(dplyr)
library(ggplot2)
x = expand.grid(name = c("D01", "D02", "D03", "D04"), component = c("F", "W", "M", "V"))
value = runif( min = 20, max = 150, nrow(x))
data2 = cbind(x, value) %>%
dplyr::arrange(name)
ggplot2::ggplot(data = data2, aes(x = name, fill = factor(component))) +
ggplot2::geom_area(aes(y = value), position = 'stack')
我阅读了问题 and ,但那里发布的解决方案并没有解决我的问题。感谢您的任何建议。
如果我们将 'x' factor
转换为 integer
,它应该可以工作
library(ggplot2)
library(dplyr)
data2 %>%
mutate(name = as.integer(name)) %>%
ggplot(aes(x = name, fill = component)) +
geom_area(aes(y = value), position = 'stack')+
scale_x_continuous(labels = levels(data2$name))
我正在尝试 geom_area 生成堆积面积图,但它生成的是入口图。这是一个例子
library(dplyr)
library(ggplot2)
x = expand.grid(name = c("D01", "D02", "D03", "D04"), component = c("F", "W", "M", "V"))
value = runif( min = 20, max = 150, nrow(x))
data2 = cbind(x, value) %>%
dplyr::arrange(name)
ggplot2::ggplot(data = data2, aes(x = name, fill = factor(component))) +
ggplot2::geom_area(aes(y = value), position = 'stack')
我阅读了问题
如果我们将 'x' factor
转换为 integer
,它应该可以工作
library(ggplot2)
library(dplyr)
data2 %>%
mutate(name = as.integer(name)) %>%
ggplot(aes(x = name, fill = component)) +
geom_area(aes(y = value), position = 'stack')+
scale_x_continuous(labels = levels(data2$name))