How to build checkmarks and numbers of y and x axes in ggplot graph?

I draw a graph using ggplot. Here is an example with the ggplot package:

df <- data.frame( gp = factor(rep(letters[1:3], each = 10)), y = rnorm(30) ) ds <- plyr::ddply(df, "gp", plyr::summarise, mean = mean(y), sd = sd(y)) ggplot(df, aes(gp, y)) + geom_point() + geom_point(data = ds, aes(y = mean), colour = 'red', size = 3) + theme( axis.text.y = element_text(hjust = 3), axis.text.x = element_text(vjust = 5), axis.ticks.length = unit(-0.25, "cm"), # length of the axis ticks ) 

Here is the result:

enter image description here

As you can see, the ticks are inside, but the numbers for the y axis are terribly aligned, and those on the X axis overlap the ticks.

So, in the end, I need the ticks inside the graph and the axis labels (numbers) inside the ggplot graph. I heard that we should use the margin tool, but I'm not sure how to specify the fields inside the chart.

Edit: you can see how when using the margin function the numbers do not align properly ... enter image description here

+5
source share
1 answer

Perhaps specifying margin inside element_text using the margin function, what are you looking for?

 ggplot(df, aes(gp, y)) + geom_point() + geom_point(data = ds, aes(y = mean), colour = 'red', size = 3) + theme( axis.text.y = element_text(margin = margin(0,-.5,0,.5, unit = 'cm')), axis.text.x = element_text(vjust = 5, margin = margin(-0.5,0,0.5,0, unit = 'cm')), axis.ticks.length = unit(-0.25, "cm") , # length of the axis ticks ) + ylim(c(min(df$y)-.5,max(df$y))) 

enter image description here

+5
source

Source: https://habr.com/ru/post/1274374/


All Articles