0

This solution seems to have died. It was answered 9 years and 10 months ago, by that time it is allowed to be replaced by several updates in between. Now, for an example I want to use the dput from this question (which is my data). My current version of the code to create a plot looks like this:

ggplot(GoatMerged, aes(date, goat_pos, color = as.factor(GoatName))) +
geom_line() +
scale_x_datetime(date_breaks = '1 day') + 
labs(color = 'Goats', x='Time', y='Positions') +
theme(axis.text=element_text(size=6)) +
#theme(aspect.ratio=16/9) +
#coord_fixed(ratio=3/4) +
#theme(axis.title.y = element_text(size=40, vjust=2)) +
#theme(axis.title.x = element_text(size=40, vjust=-0.05)) +
theme_classic()
ggsave(filename="GoatPositionBosca.pdf", device = "pdf", width = 12, height = 7, units = "cm")

The lines that are commented out indicate my other tries to get the plot to work. Now, in R GUI under windows the output looks like this:

RGUIOutPut

The output after ggsave using device = "pdf" however gives that result:

PDFOutPut

As we can see, the number labels of the x-axis overlap. Trying of diverse options using element_text like size or vjust got ignored by ggsave.

Which leads us to the question:

How to use ggsave with device = "pdf" in a way that x-axis number labels are not distorted?


Edit

Thanks to the comment from @elielink I could make some changes and by doing so also implement something else:

ggplot(GoatMerged, aes(date, goat_pos, color = as.factor(GoatName))) +
geom_line() +
scale_x_datetime(date_breaks = '1 day', guide = guide_axis(n.dodge = 2)) + 
labs(color = 'Goats', x='Time', y='Positions') +
theme_classic() +
theme(axis.text=element_text(size=6)) +
theme(legend.position = "bottom")
ggsave(filename="GoatPositionBosca.pdf", device = "pdf", width = 12, height = 9, units = "cm")

which gives that result:

PDF2OutPut

Here we can see that the PDF cuts the right side of the x-axis numbering label, thus that labeling is still distorted.

PolII
  • 107
  • 8
  • I think the element text might be ignored because there is this ultimate ```theme_classic()```after the prior theme modification. Try in this order, ```theme_classic()+ theme(axis.text=element_text(size=6)) + ggsave(filename="GoatPositionBosca.pdf", device = "pdf", width = 12, height = 7, units = "cm")``` And tell me if it worked – elielink Aug 11 '21 at 09:33

1 Answers1

0

To those who happen to have the same question as I did, I want to give here my solution that I got to after dealing some time with ggplot2. The code to create the graph is:

ggplot(GoatMerged, aes(date, goat_pos, color = as.factor(GoatName), alpha=as.factor(EarStatus))) +
geom_line() +
geom_point() +
scale_alpha_manual(values = c(0.25, 1)) +
scale_x_datetime(date_breaks = '1 day', date_labels = "%d.%m.%Y") + #, guide = guide_axis(n.dodge = 2)) +
labs(color = 'Individual', alpha='some_category', x='Time points', y='Position') +
theme_bw() +
theme(legend.position = "bottom", legend.text=element_text(size = unit(11, "pt")), legend.title=element_text(size = unit(11, "pt"), face="bold"), legend.box = "vertical", legend.direction = "horizontal") +
theme(axis.title.y = element_text(size=unit(11, "pt"), face="bold", vjust=2)) +
theme(axis.title.x = element_text(size=unit(11, "pt"), face="bold", vjust=-2)) +
theme(axis.text=element_text(size=unit(8, "pt"))) +
theme(axis.text.x = element_text(colour = "black", face="bold")) +
theme(axis.text.y = element_text(colour = "black", face="bold")) +
theme(aspect.ratio=9/16) +
theme(plot.margin = unit(c(0,1.5,0,0.5), "cm"))
ggsave(filename="GoatPosition.pdf", device = "pdf", width = 17, height = 13, units = "cm", dpi = 600)

Based on that the graph within a PDF viewer looks as follows:

PDF3OutPut

As we can see the dates do not overlap, the last date is fully on the page and not cut off and also all the labels are fully present. It turns out, setting the aspect ratio of ggplot2 in unison with the ratio expressed in width and height in ggsave is crucial to not get a distorted graph.

PolII
  • 107
  • 8