Reputation: 64074
With this code:
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(ToothGrowth, aes(x=dose, y=len, color=dose, shape=dose)) +
geom_jitter(position=position_jitter(0.2))+
labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")
p + theme_classic()
I expect to get image like this:
But how come I get this instead:
Notice the missing x-axis an y-axis line. How can I enable it?
This is theme_classic()
specific issue.
Upvotes: 6
Views: 6767
Reputation: 5532
Here is a solution from this GitHub issue
p + theme_classic() +
theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'),
axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid'))
Edit
If you are running into this issue, updating ggplot2
should fix the issue, and the solution above shouldn't be necessary.
Edit 2023-06-29
As of ggplot2 3.4.0
, the size
argument of element_line()
is deprecated and linewidth
should be used instead. However, as mentioned above, the latest version of ggplot2
should not have this issue, and the theme
addition should no longer be needed.
p + theme_classic() +
theme(axis.line.x = element_line(colour = 'black', linewidth = 5, linetype='solid'),
axis.line.y = element_line(colour = 'black', linewidth = 5, linetype='solid'))
Upvotes: 8