Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

r - double labels on x and y axis in ggplot

I want to make a scatter plot via ggplot, where both x and y axis have double values marked .. for example - x axis will show the labels - both after and before log transformation of the values.. and same for y axis..

Can anyone tell me how to do that? I am able to create a secondary axis to plot the values, but I do not want that I want both the values should be plotted in a double layer on both x and y axis: so that the upper labels labels shows log values and the lower labels shows values before log transformation..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you can do it with scale_*_continuous. You can use it to do the transformation and then you can change the labels manually (using the labels argument).

library(ggplot2)

df.data <- data.frame(x=c(1, 2, 3, 4, 5),
                      y=c(1, 10, 100, 1000, 10000))

labels.y <- paste(round(log10(df.data$y),1), df.data$y, sep='
')
labels.x <- paste(round(log10(df.data$x),1), df.data$x, sep='
')

ggplot(df.data) +
  geom_line(aes(x=x, y=y)) +
  scale_y_continuous(breaks = df.data$y,
                     trans = 'log10',
                     labels=labels.y) +
  scale_x_continuous(breaks = df.data$x,
                     trans = 'log10',
                     labels=labels.x) +
  xlab('log(x)
x') + ylab('log(y)
y') +
  theme(axis.title.y = element_text(angle=0),
        axis.title.x = element_text(angle=0,hjust=1))

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...