最佳答案
资料:
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
,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”使用 factor 方法。请看这个图。
temp$new = factor(temp$type, levels=c("T","F","P"), labels=c("T","F","P"))
然而,现在我不知道如何修正变量 size
的顺序。应该是50% ,100% 。150% 和200% 。