-1

I'm trying to increase the distance between the axis label and axis text (tick marks), but cannot find the right code how to do it. Can maybe somebody help?

 library(ggplot2)
 library(reshape2)

 data<-read.csv("trial.csv",header=TRUE,dec=".",sep=',',na.strings="NA")

 p1<-ggplot(data=data,aes(x=Year))+
     geom_line(aes(y=Cumulative),linetype="solid",color="red",size=1.1)+
     geom_point(aes(y=Cumulative),shape=1,color="red",size=3,stroke=1.5)+
     geom_line(aes(y=Annual),linetype="solid",color="darkorange",size=1.1)+
     geom_point(aes(y=Annual),shape=1,color="darkorange",size=3,stroke=1.5)+
     scale_y_continuous(sec.axis=sec_axis(~.*1/10,name="Annual"))
     p1+labs(x="Year",y="Cumulative")
     p1+theme(axis.title.x=element_text(size=18),
              axis.text.x=element_text(size=14),
              axis.title.y=element_text(size=18),
              axis.text.y=element_text(size=14),
              axis.ticks=element_blank())

Unfortunately, I don't find a solution even after googling for a while. Any suggestions are welcome!

mnm
  • 1,962
  • 4
  • 19
  • 46
OceanSun3
  • 31
  • 4
  • Can you give example? pics of what you have and what you're trying to do. – wisamb Jul 07 '19 at 23:25
  • Looks like this has been answered before: https://stackoverflow.com/questions/14487188/increase-distance-between-text-and-title-on-the-y-axis e.g. `theme(axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l = 0)))` – Jon Spring Jul 07 '19 at 23:51

1 Answers1

-1

use \n to add an empty line

so to add an empty line between your labels and graph, change p1+labs(x="Year",y="Cumulative") to

p1+labs(x="\nYear",y="Cumulative\n")

Rex Parsons
  • 199
  • 10
  • Thank you Rex for your suggestion! I ran `p1+labs(x="\nYear",y="Cumulative\n")` as part of the script, but nothing happened. Any ideas why nothing changed? – OceanSun3 Jul 08 '19 at 00:32
  • It definitely works for me when I just double checked. Perhaps try `p1+labs(x="\n\n\nYear",y="Cumulative\n\n\n")` just to make it more obvious incase its tricky to see the difference? – Rex Parsons Jul 08 '19 at 00:41
  • ohhhhhhh I just saw what was going on! In the code that you provided, you're not assigning the `p1+ labs()` statement to any new object (`p1`), so when you do the final statement to adjust the text size, you're doing so to the graph without any changes to the axis text. Surely the graph which is shown doesn't include the adjusted labels? – Rex Parsons Jul 08 '19 at 00:44
  • I just added many `\n` in the respective spots, but nothing changed? Where do you suggest to make another change in the code? It clearly didn't change anything. – OceanSun3 Jul 08 '19 at 00:52
  • `p1 <- p1+labs(x="\n\nYear",y="\n\nCumulative")` and then: `p1+theme(axis.title.x=element_text(size=18), axis.text.x=element_text(size=14), axis.title.y=element_text(size=18), axis.text.y=element_text(size=14), axis.ticks=element_blank())` – Rex Parsons Jul 08 '19 at 01:03
  • That was the clue. Thanks a lot!!! – OceanSun3 Jul 08 '19 at 01:10
  • The next issue I have is about the secondary y-axis. How can I increase the spacing between "Annual" and the tick marks and affiliate the second (orange) data set to the secondary y-axis? – OceanSun3 Jul 08 '19 at 01:11
  • hmm, I'm not quite sure how to help you with that as I don't have a good understanding of the current graphs. Can you post an image of how your graphs currently look and perhaps the head of your data being passed to ggplot? – Rex Parsons Jul 08 '19 at 01:53
  • `head(data) Year Annual Cumulative 1 1960 1 1 2 1961 0 1 3 1962 0 1 4 1963 0 1 5 1964 0 1 6 1965 0 1` – OceanSun3 Jul 08 '19 at 02:10
  • I'm actually not sure how to upload a picture here and also do format the head(data) output better for your understanding?! – OceanSun3 Jul 08 '19 at 02:11
  • hmm I don't have a great grasp about the question so I'm assuming that the axis title you're referring to us currently being called using `scale_y_continuous(sec.axis=sec_axis(~.*1/10,name="Annual"))` Therefore, if you want to add space between the title and the axis, change `"Annual"` to `"Annual\n\n"`. Hopefully that'll work. – Rex Parsons Jul 08 '19 at 07:48