la_leche
la_leche

Reputation: 473

ggplot: Log scale with linear labels

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

Upvotes: 12

Views: 13207

Answers (4)

Axeman
Axeman

Reputation: 35187

coord_trans does exactly that:

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

enter image description here

Upvotes: 8

tbradley
tbradley

Reputation: 2280

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.

Upvotes: 6

GL_Li
GL_Li

Reputation: 1798

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
    )

Upvotes: 12

David Klotz
David Klotz

Reputation: 2431

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.

Upvotes: 3

Related Questions