6

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:

enter image description here

But how come I get this instead:

enter image description here

Notice the missing x-axis an y-axis line. How can I enable it?

This is theme_classic() specific issue.

neversaint
  • 60,904
  • 137
  • 310
  • 477
  • OK, same for me: `R version 3.3.0` and `ggplot2_2.1.0` – Roman Jul 11 '16 at 14:27
  • If you look at the definition of theme_classic you'll see that both axis.line.x and axis.line.y are set to element_blank. Either change this definition yourself or you could use theme_classic2 in the package survminer. – David_B Jul 11 '16 at 14:35

1 Answers1

8

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'))

enter image description here

steveb
  • 5,382
  • 2
  • 27
  • 36