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:
The output after ggsave using device = "pdf"
however gives that result:
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:
Here we can see that the PDF cuts the right side of the x-axis numbering label, thus that labeling is still distorted.