Interesting type of visualisation. It's possible - but it's a bit of a hack.
Credit for inspiration how to handle geom_text goes to @gregor 's answer of this question.
Another challenge for this hack is to find the correct plot dimensions. R Studio won't show you the real output, and even defining the device size did not help me. Or here, a useful cheatsheet for plot sizes So therefore I did what I usually do and preview in a jpg which I produce with ggsave
. My final plot is then usually a pdf.
Further comments are in the code.
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')
The image is not output of the reprex code but just a screenshot of the image.
Created on 2019-12-06 by the reprex package (v0.3.0)