8

I wish to log transform my data but have an axis with linear values that correspond to the log ticks. For example, in page 3 of the following PDF from Iversen and Soskice 2002. The data has been transformed, but the labels are in their corresponding linear values for readability.

http://faculty.washington.edu/cadolph/vis/vishw1.pdf

Here is some reproducible data and my start to the plot:

set.seed(51)
data<-data.frame(country=letters[1:14], 
                 poverty=runif(14,min=1,max=100),
                 parties=runif(14,min=1,max=10))

ggplot(data, aes(parties, poverty))+
  geom_point(size=2)+
  scale_x_log10()

Any ideas? I have seen other similar questions but none of them have working answers (e.g. Linear Ticks on a log plot in R's GGplot).

la_leche
  • 423
  • 1
  • 7
  • 14

4 Answers4

10

Try trans inside scale_x_contineous, which transform axis only.

ggplot(data, aes(parties, poverty))+
    geom_point(size=2)+
    scale_x_continuous(
        trans = "log10",
        breaks = 1:10
    )
GL_Li
  • 1,758
  • 1
  • 11
  • 25
  • The only difference between your graph and the OP's is the `breaks = 1:10`. Switching to `scale_x_continuous` with `trans` has no difference compared to `scale_x_log10` – Michael Dewar Oct 31 '20 at 02:35
7

coord_trans does exactly that:

ggplot(data, aes(parties, poverty))+
  geom_point(size=2)+
  coord_trans(x = 'log10')

enter image description here

Axeman
  • 32,068
  • 8
  • 81
  • 94
6

This is similar to the other two answers, but gives you something a little different:

ggplot(data, aes(parties, poverty))+
  geom_point(size=2)+
  scale_x_log10(breaks = scales::log_breaks(n = 10)) +
  annotation_logticks(sides = "b")

if you want it on both the x and y axis then you need to add:

 scale_y_log10(breaks = scales::log_breaks(n = 10))

and change sides = "b" to sides = "bl" in the annotation_logticks() function.

tbradley
  • 2,210
  • 11
  • 20
3

That's what the breaks parameter covers (checkout ?scale_x_log10).

For your current question, you could modify the last line to:

scale_x_log10(breaks = 1:999)

or

scale_x_log10(breaks = 2*(1:999))  # just even numbers

etc.

David Klotz
  • 2,401
  • 1
  • 7
  • 16