【发布时间】:2019-03-19 15:16:11
【问题描述】:
这是我的数据示例。我有几个不同的传感器数据(Var1)被传感器(Var2)分解。这是一个可重现的数据集。
var.sens1 <- structure(list(Var1 = structure(c(9L, 9L, 9L, 9L, 9L, 1L, 2L,
9L, 9L),
.Label = c("bathymetry", "current", "deep scattering layer",
"frontal data", "imagery", "ocean productivity",
"salinity", "sea surface height", "sea surface temperature",
"vessel", "wind"),
class = "factor"),
Var2 = structure(c(1L, 2L, 3L, 4L, 7L, 8L, 8L, 8L, 10L),
.Label = c("AMSR-E", "AVHRR", "CZCS", "Imager",
"IRS-P4 OCM", "MERIS", "MODIS", "not specified",
"OCI", "SeaWiFS",
"SeaWinds"), class = "factor"),
Freq = c(4L, 37L, 1L, 3L, 20L, 4L, 20L, 26L, 2L)),
row.names = c(9L, 20L, 31L, 42L, 75L, 78L,79L, 86L, 108L),
class = "data.frame")
然后我使用此代码对我的数据进行分面包装(而不是堆叠条形图)。
vp <- ggplot(var.sens1, aes(x=Var2, y=Freq, fill=Var1, label=Freq)) +
geom_bar(stat="identity")
vp + facet_wrap( ~ Var1, ncol=3, scales="free_y") +
coord_flip() +
theme_light()+
theme(legend.position="none") +
xlab("Sensor") + ylab("Frequency")
我使用 scales_y=free 删除图表中任何不需要的空间。并得到以下情节。
但是,条宽大小不同。有谁知道如何在刻面包装时修复条形宽度?在 geom_bar 中修复 barwidth 似乎不起作用。也不会使用 geom_col 和 position_dodge2 并按照推荐的 here 保留。请参阅下面的代码和图像。
vp <- ggplot(var.sens1, aes(x=Var2, y=Freq, fill=Var1, label=Freq)) +
geom_col(position = "dodge")
vp +
facet_grid(~Var1, scales = "free_y")+
coord_flip() +
theme_light()+
theme(legend.position="none") +
xlab("Sensor") + ylab("Frequency")
我更喜欢在 facet_wrap 上使用 facet_wrap,这样我就没有一长串的图(我有多个变量需要在我的数据集中绘制,而不仅仅是显示的三个)。
【问题讨论】:
-
我尝试了位置闪避,但似乎没有用。
-
为什么?您收到的错误消息是什么?
-
没有错误信息。只是不会改变宽度,所以它们匹配。它也使用了 facet_grid,而且我有多个变量,所以 facet_wrap 更适用。
-
没有错误信息。它会改变宽度,但不会移除空传感器。请参阅问题更新。
标签: r ggplot2 bar-chart geom-bar