neversaint
neversaint

Reputation: 64074

How to enable x-axis and y-axis line in GGPLOT theme_classic()

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.

Upvotes: 6

Views: 6767

Answers (1)

steveb
steveb

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

enter image description here

Upvotes: 8

Related Questions