[英]Issue in combining stacked bar and line graph in R using ggplot2
library(ggplot2)
library(reshape2)
data <- data.frame(partition = c("1", "2", "3", "4","5"),
edge=c(2914.2025,4274.438333,7072.29,7984.68,10232.96333),
cloud=c(11445.02,10384.94,9165.71,7884.15,7113.79),
communication=c(803345.0248,805614.764,810357.3823,460484.3287,483277.6666))
df2 <- data.frame(partition = c("1", "2", "3", "4","5"),output_data=c(199.1,199.1,199.1,99.5,99.5))
elections_long <- melt(data, id = "partition")
ggplot(elections_long, aes(x = partition, y = value)) +
geom_bar(stat="identity", aes(fill = variable))+geom_line(data=df2, aes(x=partition, y=value),
colour="blue")
我在 R 中繪制圖表時遇到了一些問題,就像下面堆疊條形圖中表示的圖表一樣。
邊緣、雲和通信在堆積條中表示,而 output 數據應表示為折線圖。
您可以獲取長格式數據,創建堆疊條形圖並使用df2
中的數據在其頂部添加一條線。
library(ggplot2)
tidyr::pivot_longer(data, cols = -partition) %>%
ggplot() + aes(x = partition, y = value) +
geom_col(aes(fill = name)) +
geom_line(data=df2, aes(x=partition, y=output_data, group = 1), colour="blue")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.