204*_*204 5 plot r bar-chart ggplot2
我想要一个圆形条形图,如下所示:
为此,我在 R 中使用了以下代码:
require(ggplot2)
ggplot(PolarPlot,aes(x,y,
fill=x))+
geom_bar(width=1,stat="identity")+
coord_polar() + xlab("")+ylab("")+
theme(legend.position = "none" , axis.text.y = element_blank() ,
axis.ticks = element_blank()
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我需要进行哪些修改才能获得所需的图表吗?
数据如下:
PolarPlot <- structure(list(x = structure(1:7, .Label = c("Class1", "Class2",
"Class3", "Class4", "Class5", "Class6", "Class7"), class = "factor"),
y = c(2L, 8L, 17L, 56L, 28L, 7L, 2L)), .Names = c("x", "y"),
class = "data.frame", row.names = c(NA, -7L))
Run Code Online (Sandbox Code Playgroud)
您首先创建一个条形图,然后将其转换为极坐标(这很好)来生成您的图。如果您不希望条形图从极坐标图中的中心开始,那么您需要确保它们不会从条形图中的底部开始。
我的意思最容易通过实际展示来解释。首先,我以与您相同的方式创建条形图,但我扩展了 y 轴以达到负值:
p <- ggplot(PolarPlot, aes(x, y, fill=x)) +
geom_bar(width=1,stat="identity") +
xlab("") + ylab("") +
theme(legend.position = "none" , axis.text.y = element_blank() ,
axis.ticks = element_blank()) +
scale_y_continuous(limits = c(-50, 60))
Run Code Online (Sandbox Code Playgroud)
当我切换到极坐标时,条形图下端的间隙转换为极坐标图中心的间隙:
p + coord_polar()
Run Code Online (Sandbox Code Playgroud)
您可以通过使用limits
in 中使用的值来修改中心间隙的大小scale_y_continuous()
。
归档时间: |
|
查看次数: |
2914 次 |
最近记录: |