数据:
df <- data.frame(
type = c("T", "F", "P", "T", "F", "P", "T", "F", "P", "T", "F", "P"),
size = c("50%", "50%", "50%", "100%", "100%", "100%", "150%", "150%", "150%", "200%", "200%", "200%"),
amount = c(48.4, 48.1, 46.8, 25.9, 26, 24.9, 21.1, 21.4, 20.1, 20.8, 21.5, 16.5)
)
我需要使用 ggplot (x-axis -> type
, y-axis -> amount
, group by size
) 绘制上述数据的条形图。当我使用以下代码时,我没有得到变量type
以及size
数据中显示的顺序。请看图。我为此使用了以下代码。
ggplot(df, aes(type, amount , fill=type, group=type, shape=type, facets=size)) +
geom_col(width=0.5, position = position_dodge(width=0.6)) +
facet_grid(.~size) +
theme_bw() +
scale_fill_manual(values = c("darkblue","steelblue1","steelblue4"),
labels = c("T", "F", "P"))
.
为了解决订单问题,我使用以下方法对变量“type”使用了因子方法。也请看图。
temp$new = factor(temp$type, levels=c("T","F","P"), labels=c("T","F","P"))
但是,现在我不知道如何修复变量的顺序size
。它应该是 50%、100%。150% 和 200%。