大小范围内堆叠条形图的R函数
我要为大小范围创建条形图。我提供了一个虚拟数据集(数据) 如果只有一个“大小”,我可以创建一个绘图。例如,如果只有“Size2”,我会这样做大小范围内堆叠条形图的R函数,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,我要为大小范围创建条形图。我提供了一个虚拟数据集(数据) 如果只有一个“大小”,我可以创建一个绘图。例如,如果只有“Size2”,我会这样做 library(reshape2) data1 <- melt(data, id.var="Size2") library(ggplot2) ggplot(data1, aes(x = Size2, y = value, fill = variable)) + geom_bar(stat = "identity") library(重塑2)
library(reshape2)
data1 <- melt(data, id.var="Size2")
library(ggplot2)
ggplot(data1, aes(x = Size2, y = value, fill = variable)) +
geom_bar(stat = "identity")
library(重塑2)
数据1一种方法是:
size1 <- c(0,5,10,20,50)
size2 <- c(5,10,20,50,100)
A = c(0.3,0.1,0.5,0.2,0.7)
B <- c(0.5,0.2,0.2,0.4,0.1)
C <- c(0.2, 0.7,0.3,0.4,0.2)
data <- data.frame(Size1=factor(size1), # this removes gaps between bars in x axis)
Size2=factor(size2),
A=A,
B=B,
C=C)
library(reshape2)
library(dplyr)
data1 <- melt(data[,c("Size1","A","B","C")], id.var="Size1")
data2 <- melt(data[,c("Size2","A","B","C")], id.var="Size2")
library(ggplot2)
g1 <- ggplot(data1, aes(x = Size1, y = value, fill = variable,)) +
geom_bar(stat = "identity", show.legend = FALSE)
g2 <- ggplot(data2, aes(x = Size2, y = value, fill = variable)) +
geom_bar(stat = "identity")
g1 <- ggplotGrob(g1)
g2 <- ggplotGrob(g2)
g <- cbind(g1, g2)
library(grid)
grid.newpage()
grid.draw(g)
size1使用data$Size=paste(data$size1,data$Size2,sep=“-”)
创建指示范围的单个大小列(熔化时放下其他大小列)。要使条形连接,请在geom_-bar
@Gregor thank you中设置width=1
。它适用于这些数据。但对于我的数据,我需要将我的大小更改为对数刻度,并为每个大小范围创建一个条。有没有办法不用'sep=“-”`?geom_条
就可以很好地处理分类x轴。如果要使用连续x轴,我建议使用geom\u rect
。但在这种情况下,我认为你需要自己计算百分比。我认为OP需要一个条形图,其中x轴上的条形位置从size1
到size2
(即geom\u rect
图)。不是两个不同的酒吧区。@Tianyu Zhang谢谢你,但正如Gregor正确指出的,我要找的是一个酒吧区
size1 <- c(0,5,10,20,50)
size2 <- c(5,10,20,50,100)
A = c(0.3,0.1,0.5,0.2,0.7)
B <- c(0.5,0.2,0.2,0.4,0.1)
C <- c(0.2, 0.7,0.3,0.4,0.2)
data <- data.frame(Size1=factor(size1), # this removes gaps between bars in x axis)
Size2=factor(size2),
A=A,
B=B,
C=C)
library(reshape2)
library(dplyr)
data1 <- melt(data[,c("Size1","A","B","C")], id.var="Size1")
data2 <- melt(data[,c("Size2","A","B","C")], id.var="Size2")
library(ggplot2)
g1 <- ggplot(data1, aes(x = Size1, y = value, fill = variable,)) +
geom_bar(stat = "identity", show.legend = FALSE)
g2 <- ggplot(data2, aes(x = Size2, y = value, fill = variable)) +
geom_bar(stat = "identity")
g1 <- ggplotGrob(g1)
g2 <- ggplotGrob(g2)
g <- cbind(g1, g2)
library(grid)
grid.newpage()
grid.draw(g)