我有这样的数据
df<- structure(list(name = structure(c(2L, 12L, 1L, 16L, 14L, 10L,
9L, 5L, 15L, 4L, 8L, 13L, 7L, 6L, 3L, 11L), .Label = c("All",
"Bab", "boro", "bra", "charli", "delta", "few", "hora", "Howe",
"ist", "kind", "Kiss", "myr", "No", "TT", "where"), class = "factor"),
value = c(1.251, -1.018, -1.074, -1.137, 1.018, 1.293, 1.022,
-1.008, 1.022, 1.252, -1.005, 1.694, -1.068, 1.396, 1.646,
1.016)), .Names = c("name", "value"), class = "data.frame", row.names = c(NA,
-16L))
如下所示
# name value
#1 Bab 1.251
#2 Kiss -1.018
#3 All -1.074
#4 where -1.137
#5 No 1.018
#6 ist 1.293
#7 Howe 1.022
#8 charli -1.008
#9 TT 1.022
#10 bra 1.252
#11 hora -1.005
#12 myr 1.694
#13 few -1.068
#14 delta 1.396
#15 boro 1.646
#16 kind 1.016
当我绘制它时
ggplot(df, aes(x = 1,y = name, fill = value)) +
geom_tile() +
ylab("")
它随机绘制
但我想拥有与我的数据相同的顺序。我也想让ylim更小,但我做不到。我感谢任何建议
答案 0 :(得分:1)
问题是您的名称因素的级别是按字母顺序排序 。您想要做的是重新排序name
的级别。在情节之前这样做:
df$name <- factor(df$name, levels = df$name)
这将按原样从下到上打印值。 对于从上到下的订购,请使用
df$name <- factor(df$name, levels = rev(df$name))
最后,设置“ylim”在这里没有意义。您可能希望降低画布的高度(例如,通过更改RStudio中“绘图”窗格的大小)。