我正在制作一张柱状图,展示不同类型的猎物在巢穴中所占比例。
我的数据如下:
Prey <- c(rep("Bird", 12), rep("Lizard", 3), rep("Invertebrate", 406))
Type <- c(rep("Unknown bird", 12), rep("Skink", 2), rep("Gecko", 1),
rep("Unknown Invertebrate", 170), rep("Beetle", 1),
rep("Caterpillar", 3), rep("Grasshopper", 3), rep("Huhu grub", 1),
rep("Moth", 34), rep("Praying mantis", 1), rep("Weta", 193))
Preydata <- data.frame(Prey,Type)
ggplot(Preydata, aes(x = Prey, y = (..count..)/sum(..count..))) +
scale_y_continuous(labels = percent_format()) +
geom_bar(aes(fill = Type), position = "dodge")
我的图像看起来像下面的情节。
我希望所有“type”条形宽度都相同,但当我在 geom_bar
下更改宽度时,只会更改“prey”条的宽度。当我尝试使用以下内容时:
ggplot(Preydata, aes(x = as.numeric(interaction(Prey, Type)),
y = (..count..)/sum(..count..))) +
scale_y_continuous(labels = percent_format()) +
geom_bar(aes(fill = Type), position = "dodge")
geom_bar(position = position_dodge(preserve = "single"))
。 - PatrickT