Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot: Log scale with linear labels

Tags:

r

ggplot2

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

like image 245
la_leche Avatar asked Jan 17 '18 18:01

la_leche


3 Answers

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
    )
like image 60
GL_Li Avatar answered Oct 16 '22 14:10

GL_Li


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.

like image 6
tbradley Avatar answered Oct 16 '22 12:10

tbradley


coord_trans does exactly that:

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

enter image description here

like image 6
Axeman Avatar answered Oct 16 '22 13:10

Axeman