2

Plot for types of exericse vs mental healthHi, Attached is the graph I plotted for the type of sports vs. average number of days being depressed for 30 days (with variables ranging from 0 to 30). I am trying to get the y axis more spread out but to no avail. I have tried adding the "height" in my code but nothing changes. I wonder why is that?

As you can see on the y axis, the dots are all squished together. I want to get them more spread out on the same scale (0 to 30).

Thanks!

ggplot(data=question2data, aes(x=exract11, y=menthlth), height = 500 , width = 7)+ geom_point(position = position_dodge(1))+ theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

4
  • Have you tried increasing the height of your graphics device (usually it's best to use a file device if you want reproducible output, e.g., using ggsave)? Commented Jul 19, 2019 at 6:22
  • I am not sure how to use ggsave to change height? is it just ggsave(height= ' ') ? Commented Jul 19, 2019 at 6:25
  • Yes, but you should also specify the width (and if you save in a raster graphics format the resolution). See help("ggsave"). Commented Jul 19, 2019 at 6:30
  • Hi, I tried the ggsave command and it did adjust the size of the graph. However it saves directly to the path so I couldn't see it in the console anymore. Is there a way around this? Commented Jul 22, 2019 at 1:34

1 Answer 1

2

If you are saving the plot you can adjust how the plot and axis labels plot using height within ggsave - larger plot sizes give smaller labels and more space to the plot itself.

You can also change the font size directly within the theme (see p2 below).

To see the options you can look at these four plots (using iris dataset as you did not provide data)

Warning! This will save plots to 'c:/temp' assuming you have one. Or fail if you don't.

library(ggplot2)

p1 <- ggplot(iris, aes(x = Species, y = Sepal.Width))+geom_point()+ 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))



ggsave(p1, filename = "c:/temp/test.png",height = 10, width = 5)

ggsave(p1, filename = "c:/temp/test2.png",height = 5, width = 5)

p2 <- p1 + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, size = rel(0.5)))
ggsave(p2, filename = "c:/temp/test_adjustedTextSize.png",height = 10, width = 5)

ggsave(p2, filename = "c:/temp/test2_adjustedTextSize.png",height = 5, width = 5)
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks so much @user2738526 ! Question: the filename is the file path of the image of the plot itself right ? Or is it a different path?
filename is where you want to save it to. What I have as p1 or p2 actually the argument plot is the object that you want to save. Does that answer the question, I'm not 100% sure what you mean?
If I use ggsave, the plot gets saved directly to the filename. Is there a way to use ggsave() for adjusting size while still able to see it in the console?
As I said in my answer, if you want to change the font size directly, assuming your problem is with the x axis text (so its viewable in the console) use theme: theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, size = rel(0.5))). I.e. just call p2 directly from my example
I want to change the height, not just the size of the text
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.