User878239
User878239

Reputation: 719

Add axes with ticks and without labels in ggplot

I got the following chart in ggplot:

library(ggplot2)
a <- seq(1,20)
b <- a^0.25
df <- as.data.frame(cbind(a,b))

ggplot(df, aes(x = a, y = b)) + geom_point()

Question: How can I add axes to the top and to the right of the plot, with ticks, but not with labels (as the labels are on the bottom and on the left already), and also with white background? If I use + theme_classic(), the background becomes white, but the axes on the top and right are also removed.

Upvotes: 0

Views: 866

Answers (1)

astrofunkswag
astrofunkswag

Reputation: 2718

I think you're looking for dup_axis

ggplot(df, aes(x = a, y = b)) + 
  geom_point() +
  scale_x_continuous(sec.axis = dup_axis(name = NULL, labels = NULL)) + 
  scale_y_continuous(sec.axis = dup_axis(name = NULL, labels = NULL)) +
  theme_classic()

enter image description here

EDIT: I wasn't clear on whether you wanted tick labels, you can add them back by removing the labels = NULL

Upvotes: 2

Related Questions