简体   繁体   中英

log transform X axis R

I have the following raw data that I plotted in R:

在此处输入图像描述

And I would like to edit this plot to look like this version below which was made by log-transforming the X axis using Excel

在此处输入图像描述

However, when I run my code below using scale_x_log10(), the output is not the desired plot I was hoping to make. See image below:

在此处输入图像描述

Can anyone identify where I have gone wrong?

ggplot(data = data, aes(x = x, y = y, group = group, color = group)) +
  stat_summary(fun = "mean", geom = "line", size = 1.2, aes(group = group, linetype = group, color = group)) +
  stat_summary(fun = "mean", geom = "point", size = 3, aes(color = group)) +
  theme_apa() +
  scale_linetype_manual(values = c("solid", "dashed")) +
  scale_color_manual(values = c("mediumturquoise", "red")) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 10)) +
  scale_x_log10(limits = c(.01, 40), breaks = c(.01, .1, 1, 10)) 

It looks like your first datapoint is at zero - this can't be displayed on a log scale. You'll need to work out if there's a difference in you data in excel, failing that you could achieve a similar result by modifying the lowest value of x with:

ggplot(data = data, aes(x = pmax(x,0.01), y = y, group = group, color = group)) +
  stat_summary(fun = "mean", geom = "line", size = 1.2, aes(group = group, linetype = group, color = group)) +
  stat_summary(fun = "mean", geom = "point", size = 3, aes(color = group)) +
  theme_apa() +
  scale_linetype_manual(values = c("solid", "dashed")) +
  scale_color_manual(values = c("mediumturquoise", "red")) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 10)) +
  scale_x_log10(limits = c(.01, 40), breaks = c(.01, .1, 1, 10)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM