有趣的可视化类型。这是可能的 - 但它有点像黑客。
对于如何处理 geom_text 的灵感,请访问 @gregor 的 answer of this question。
这个 hack 的另一个挑战是找到正确的地块尺寸。 R Studio 不会向您显示真正的输出,甚至defining the device size 也没有帮助我。 Or here, a useful cheatsheet for plot sizes 所以我做了我通常做的事情,并在我用ggsave 制作的 jpg 中预览。我的最终情节通常是 pdf。
更多的 cmets 在代码中。
library(ggplot2)
mydf <- read.so::read.so('chjoc 8.4
dejcp 5.03
memse 13.41
phjgr 5.86
altj 7.26
bujbv -9.53
maemj -10.59
poejpr -9.72
soajs -10.59
bijmb -7.39', header = FALSE)
#I use the fantastic read.so package for reading data which has been posted not ideally
mydf$value <- 0 # create a value 0 column for the geom_col hack to come
mydf$V2fac <- as.factor(mydf$V2)
#factorise your values so that your plot is in the right order,
# and the variable is categorical rather than continuous
p <- ggplot(mydf) +
geom_col(aes(x = V2fac, y = 0), color = 'black') +
# that's the hack - using color ensures that you will get a line at 0. You can change it's thickness with the size argument
geom_text(aes(V2fac, y = 0.2, hjust = 0, label = V1, group = V1), position = position_dodge(0.5)) +
scale_y_continuous(expand = expand_scale(mult = c(.1, 10))) +
# here is a lot happening.
# You need group for dodging (you have some categories with the same value).
# You want the labels aligned - therefore hjust.
# You don't want the labels to be cut by the plot borders - therefore the expand argument.
# y = 0.2 is basically chosen randomly
coord_flip() + #that's obviously the key thing to switch all at the end.
theme(axis.title = element_blank(),
axis.text.x = element_blank(),
panel.background = element_blank(),
panel.grid = element_blank(),
axis.ticks = element_blank())
# note that using theme after coord_flip is used for the newly assigned x/y axis
# ggsave(plot = p, width = 1.2, height = 5, filename = 'p.jpg')
图片不是reprex代码的输出,只是图片的截图。
由reprex package (v0.3.0) 于 2019 年 12 月 6 日创建