用这个怎么样?
library(tidyverse)
d <- structure(list(Section = structure(1:6, .Label = c("A", "B",
"C", "D", "E", "F"), class = "factor"), Env. = c(8.38, 11.84,
16.69, 11.04, 19.56, 17.93), Ar. = c(8.76, 13.51, 16.49, 11.62,
16.79, 21.34), Width = c(7L, 11L, 17L, 9L, 20L, 19L), Length = c(36L,
57L, 87L, 44L, 106L, 98L)), .Names = c("Section", "Env.", "Ar.",
"Width", "Length"), class = "data.frame", row.names = c(NA, -6L))
d %>%
gather(key, value,-Section) %>%
ggplot(aes(Section, value, colour=key, group= key)) +
geom_line(size=1.1) + geom_point(size=4)+
scale_y_continuous(name="Env_Ar",
sec.axis = sec_axis(~., name = "Width_Length"))
您还可以尝试使用不同的分面进行"free_y"
缩放。这是 IMO 更加干净和优雅。
d %>%
gather(key, value,-Section) %>%
mutate(group=ifelse(key %in% c("Width","Length"), 2, 1)) %>%
ggplot(aes(Section, value, colour=key, group= key)) +
geom_line(size=1.1) + geom_point(size=4)+
facet_wrap(~group, scales = "free_y")
编辑
这是右 y 轴的不同缩放比例(高 10 倍)的方法
d %>%
mutate(Width=Width*10,
Length=Length*10) %>%
gather(key, value,-Section) %>%
ggplot(aes(Section, value, colour=key, group= key)) +
geom_line(size=1.1) + geom_point(size=4)+
scale_y_continuous(name="Env_Ar",
sec.axis = sec_axis(~.*10, name = "Width_Length"))