K. Maya
K. Maya

Reputation: 299

Fix log plot in R

I want a plot with a log scale in x-axis and a "normal" y-axis.

I got a plot but I see a strange thing on my y-axis which I couldnt figure out.

breaks <- 10^(-10:10)
minor_breaks <- rep(1:9, 21)*(10^rep(-10:10, each=9))
ggplot(mtcars, aes(mpg, disp)) +
  geom_line(size = 1, color = "blue") +
  scale_x_log10(breaks = breaks, minor_breaks = minor_breaks, limits = c(0.1,50)) +
  annotation_logticks()

enter image description here

What is this thing on the y-axis and how can I get rid of it?

Upvotes: 1

Views: 227

Answers (2)

neilfws
neilfws

Reputation: 33782

By default annotation_logticks() adds ticks to the bottom and left axis. You don't want ticks on the left (y-axis) because it isn't log-scaled. That's why it looks wrong.

Use the sides = argument to label only the bottom (x-axis):

g <- ggplot(mtcars, aes(mpg, disp)) +
  geom_line(size = 1, color = "blue") +
  scale_x_log10(breaks = breaks, minor_breaks = minor_breaks, limits = c(0.1, 50)) +
  annotation_logticks(sides = "b")

Upvotes: 1

mcz
mcz

Reputation: 587

Using the code below you can remove the black mess on the y-axis because it's the annotated tick marks you've added. Setting side = "b" indicates that you only want the tick marks on the x-axis (the bottom).

breaks <- 10^(-10:10) 
minor_breaks <- rep(1:9, 21)*(10^rep(-10:10, each=9))

g <- ggplot(mtcars, aes(mpg, disp)) +
  geom_line(size = 1, color = "blue") +
  scale_x_log10(breaks = breaks, minor_breaks = minor_breaks, limits = c(0.1,50)) +
  annotation_logticks(sides = "b")
g

Upvotes: 5

Related Questions