数据:
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轴->type
,y轴->amount
,按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%。
labeller
更改条带标签? - just_rookie