move label of y axis horizontally

1,392 views
Skip to first unread message

felicit...@googlemail.com

unread,
Apr 16, 2016, 9:14:26 AM4/16/16
to ggplot2
dear all:
with the following script I wanna generate a barplot with the ggplot2 package:

1 ggplot(PENC,aes(x=X,y=MEAN))+geom_bar(stat='identity',         
2 width=0.5,color='black',size=1.5,fill=c('red','blue'))+theme_classic()+ 
3 geom_errorbar(aes(ymin=MEAN,ymax=MEAN+sd),width=0.1,size=1)+
4 labs(y=expression(paste('no. of mussels consumed' ,' ',crab^-1,' ',h^-8)),x='')+
5 theme(axis.text.x=element_text(size=15))+
6 theme(axis.text.y=element_text(size=15))+  
7 theme(axis.title.x=element_text(size=20))+
8 theme(axis.title.y=element_text(size=20))+ 
9 theme(plot.margin=unit(c(1,1,1,1),"cm")) 

I wanna move the y label text horizontally but with "hjust", it does not work.

If I am writing:

...
8 theme(axis.title.y=element_text(size=20,angle=90.000001,hjust=-0.5,vjust=-0.4)
...

then I am very close to how it should look like. But I guess that is not the right solution. Could you help me?

Roman Luštrik

unread,
Apr 16, 2016, 2:56:53 PM4/16/16
to felicit...@googlemail.com, ggplot2
Yan you provide a reproducible example?

Also note that you don't need to call theme a zillion times. Something along the lines of the below example should do.

theme(axis.text.x = element_text(size = 15),
      axis.text.y = element_text(size = 15),
      axis.title.x = element_text(size = 15),
      axis.title.y = element_text(size = 15),
      ...
) +


Cheers,
Roman

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
In God we trust, all others bring data.

Dennis Murphy

unread,
Apr 16, 2016, 8:29:34 PM4/16/16
to felicit...@googlemail.com, ggplot2
1. Your example is not reproducible. Google "R reproducible example"
and check out several of the top half dozen or so hits to learn how to
write an effective help request on R-related groups.

2. See https://github.com/hadley/ggplot2/issues/1435, as this issue
has come up before. Hadley developed the margin() function and the
margin = argument in element_text() to deal with the problems created
by vjust and hjust. Check out the help page of ggplot2::margin for
details; in this case, you want to use l (left) and r (right). Here is
a toy example to illustrate, adding a title to show how the t (top)
and b (bottom) arguments work, which also apply to x-axis titles.

DF <- data.frame(x = seq(10), y = seq(10))
p <- ggplot(DF, aes(x = x, y = y)) + geom_point(size = 3) +
ggtitle("A plot title")
p

# Default unit for margin() is points
p + theme(axis.title.y = element_text(margin = margin(l = 0, r = 10))
p + theme(axis.title.y = element_text(margin = margin(l = 10, r = 10))
p + theme(plot.title = element_text(margin = margin(b = 20)))
p + theme(plot.title = element_text(margin = margin(t = 20, b = 20)))


3. Another dynamite plot...sigh. Plot errorbars with a point geom at
the mean instead. It conveys the same message, only more cleanly and
with far less ink. If you need to see y = 0 in the plot, then set the
limits in scale_y_continuous() accordingly.

Dennis
Reply all
Reply to author
Forward
0 new messages