library(tidyverse)
library(ggplot2)
values<- c("v","c")
G1<- c(1,5,1,4,0,5,0,5,1,2,0,7,5,5,1,2,1,1,5,6,2,4,0,3,0,3,3,17)
G2<- c("A","A","B","B","C","C","D","D","E", "E", "F", "F", "G","G", "H","H","I", "I","J", "J", "K","K", "L", "L","M", "M", "N", "N")
temperature <-c(-0.81,-0.81,-0.91, -0.91,-1.01, -1.01,-0.99, -0.99,-0.92,
-0.92,-0.82,-0.82, -0.67,-0.67,-0.43, -0.43, -0.14,-0.14,
0.16,0.16, 0.48, 0.48, 0.77,0.77, 1.07, 1.07, 1.32,1.32)
data1<- data.frame(x = G2, y= G1, values)
ylim.prim <- c(0, 20)
ylim.sec <- c(-1.5,1.5)
b <- diff(ylim.prim)/diff(ylim.sec)
a <- (ylim.prim[1] - b*ylim.sec[1])
ggplot(data1,mapping = aes(x = x, y=G1, fill = values))+
geom_bar(stat = "identity",color = "black") +
scale_fill_manual(name = "", values =c("purple", "green"),
labels = c("EMPTY", "FULL")) +
geom_point(aes(y = a + temperature*b))+
theme_classic()+
theme(axis.title.y = element_text(size = 10,color = "black", face = "italic"))+
theme(axis.text.x = element_text(color = "black",size = 7,angle=90),axis.text.y = element_text(color = "black",size = 8,hjust = 1))+
theme(axis.title.x = element_blank()) +geom_line(aes(y = a + temperature*b),group=1, color = "#483D8B",lwd=1)+
geom_line(aes(y = a + temperature*b, group=1,color = "temperature")) +
scale_color_manual(NULL, values = "black")+
geom_point(aes(y = a + temperature*b, group=1,color = "temperature"))+
scale_y_continuous(sec.axis = sec_axis(~(. - a)/b,breaks = c(-1.3,-1.1,-0.9,-0.5,0, 0.5,0.9,1.1,1.3), name = "temperature")) +
theme(legend.background = element_rect(fill = "transparent"),
legend.box.background = element_rect(fill = "transparent", colour = NA),legend.key = element_rect(fill = "transparent"),
legend.spacing = unit(-1, "lines"))+
expand_limits(y = c(0,0))+
labs(title="", x=G2, y="G")
字符串
1条答案
按热度按时间9rbhqvlz1#
您只需将
fill = values
从主ggplot调用中移到geom_bar
调用中。这意味着只有geom_bar
调用中的draw键将用于fill
的图例中。还要注意的是,
geom_bar(stat = 'identity')
只是geom_col()
的一个很长的写法。代码组织得很差,有一些不必要的重复,所以我在这里整理了一下。
字符串
的数据