简体   繁体   English

调整 ggplot2 R 中次 y 轴的值

[英]Adjust the values of secondary y axis in ggplot2 R

I am trying to plot the data using two y axis.我正在尝试使用两个 y 轴 plot 数据。

p <- ggplot(mydf, aes(x = pos)) +
  geom_line(aes(y = data1, colour="data1")) +
  geom_line(aes(y = data2, colour="data2")) +
  scale_y_continuous(limits = c(0.40, 0.90), breaks=c(0.40, 0.050, 0.60, 0.70, 0.80, 0.90), sec.axis = sec_axis(~., name = "data1")) +
  labs(y = "freq",
       x = "position",
       colour = "") +
  scale_colour_hue(labels = c("data1",
                              "data2"),
                   l = 40) +
  ggtitle("title of the plot") +
  theme_bw() +
  theme(text=element_text(size=8)) +
  theme(plot.title = element_text(hjust = 0.5, size =8)) +
  theme(legend.direction = "horizontal", legend.position = "bottom", legend.box = "vertical")
plot(p)

The first y axis values range from 0.40-0.90 , che second y axis values range rom 0.50-0.60 .第一个 y 轴值范围为0.40-0.90 ,第二个 y 轴值范围为0.50-0.60 How to adjust the plot?如何调整 plot?

pos  data1  data2
1  0.9  0.6
2  0.8  0.6
3  0.8  0.6
4  0.7  0.6
5  0.6  0.5
6  0.5  0.5
7  0.4  0.5

mydf <- structure(list(win_mid = 88:97, emboss = c(0.8189, 0.81395, 0.818533333333333, 
0.820825, 0.81846, 0.816883333333333, 0.815757142857143, 0.8149125, 
0.814255555555556, 0.81373), clc = c(0.5985568621, 0.5985568621, 
0.598507734833333, 0.59852001665, 0.59852738574, 0.5986134848, 
0.598590549371429, 0.598623096925, 0.598573214244444, 0.59846694016
)), row.names = 76:85, class = "data.frame")

Plotting secondary y-axis on ggplot2 is more of a mathematical problem than a plotting one.ggplot2上绘制辅助 y 轴与其说是绘图问题,不如说是一个数学问题。 In general we need to take one set of values and fit them between a min and max value.一般来说,我们需要取一组值并将它们拟合在最小值和最大值之间。 This answer has the general formula https://stackoverflow.com/a/5295202/3962914 which can be implemented in this case as -该答案具有通用公式https://stackoverflow.com/a/5295202/3962914在这种情况下可以实现为 -

library(ggplot2)

ggplot(mydf, aes(x = pos)) +
  geom_line(aes(y = data1, colour="data1")) +
  geom_line(aes(y = data2, colour="data2")) +
  scale_y_continuous(limits = c(0.40, 0.90), 
                     breaks=c(0.40, 0.050, 0.60, 0.70, 0.80, 0.90), 
                     sec.axis = sec_axis(~{
                       a <- min(mydf$data2)
                       b <- max(mydf$data2)
                       (((b-a) * (. - min(.)))/diff(range(.))) + a
                     }, name = "data1")) +
  labs(y = "freq",
       x = "position",
       colour = "") +
  scale_colour_hue(labels = c("data1","data2"),
                   l = 40) +
  ggtitle("title of the plot") +
  theme_bw() +
  theme(text=element_text(size=8)) +
  theme(plot.title = element_text(hjust = 0.5, size =8)) +
  theme(legend.direction = "horizontal", 
        legend.position = "bottom", 
        legend.box = "vertical")

在此处输入图像描述


On the dput data:dput数据上:

ggplot(mydf, aes(x = win_mid)) +
  geom_line(aes(y = emboss, colour="emboss")) +
  geom_line(aes(y = clc, colour="clc")) +
  scale_y_continuous(limits = c(0.40, 0.90), 
                     breaks=c(0.40, 0.050, 0.60, 0.70, 0.80, 0.90), 
                     sec.axis = sec_axis(~{
                       a <- min(mydf$clc)
                       b <- max(mydf$clc)
                       (((b-a) * (. - min(.)))/diff(range(.))) + a
                     }, name = "emboss")) +
  labs(y = "freq",
       x = "position",
       colour = "") +
  scale_colour_hue(labels = c("emboss","clc"),
                   l = 40) +
  ggtitle("title of the plot") +
  theme_bw() +
  theme(text=element_text(size=8)) +
  theme(plot.title = element_text(hjust = 0.5, size =8)) +
  theme(legend.direction = "horizontal", 
        legend.position = "bottom", 
        legend.box = "vertical")

在此处输入图像描述

data数据

mydf <- structure(list(pos = 1:7, data1 = c(0.9, 0.8, 0.8, 0.7, 0.6, 
0.5, 0.4), data2 = c(0.6, 0.6, 0.6, 0.6, 0.5, 0.5, 0.5)), 
class = "data.frame", row.names = c(NA, -7L))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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