在 ggplot 中安装双 Y 轴(条形图 [SPI 函数] - 线图 [作物产量])
Fit double Y-axis in ggplot (barplot[SPI function] - lineplot [crop Yield])
我想用我的 SPI 函数和马铃薯产量创建一个双图,以查看它们之间的关系。我只是试试这个,但是 "y" 值不适合彼此
产量数据在这里 <- enter link description here
SPI_data 这里 <- enter link description here
library(ggplot2)
library(tidyverse)
anhos <- paste(c(1965:2018), "12", "01", sep = "-")
reemp <- paste(c(1965:2018), "01", "01", sep = "-")
yield_dt <- fread(rendi_MINAGRI.txt, header = T)
spei <- fread("ho_000253.txt", header = T)
spei <- spei[spei$YEAR %in% anhos]
spei$YEAR <- as.data.table(reemp)
spei$YEAR <- as.Date(spei$YEAR)
DF <- spei %>% dplyr::mutate(sign = ifelse(SPEI >= 0, "pos", "neg"))
DF$Rendi <- dt_rendi[,c("ANCASH")]/1000
pl <- ggplot(data = DF, mapping = aes(x = YEAR, y = Rendi, group = 1)) +
geom_line(aes(color="Rendimiento")) +
geom_point(aes(color="Rendimiento"))
pl <- pl + geom_bar(aes(y = SPEI, col = sign, fill = sign), show.legend = F, stat = "identity")+
scale_color_manual(values = c("Rendimiento" = "green", "pos" = "darkblue", "neg" ="red"))+
scale_fill_manual(values = c("Rendimiento" = "green" ,"pos" = "darkblue", "neg" = "red"))
pl <- pl + scale_y_continuous("Rendimiento Papa [tn/ha]",sec.axis = sec_axis(~., name =
toupper(indx))) +
scale_x_date(date_breaks = "2 years", date_labels = "%Y ", expand = c(0,0))
png(filename = paste0("SPEI",".png"), width = 2800, height = 1200, units = "px", pointsize = 9,res = 250 )
print(pl)
最后我有这样的东西 enter link description here,正如你所看到的,我需要一些适合两个轴的东西来比较,我不需要左右轴上的相同标签。我希望有人能帮助我。
谢谢。
感谢您没有回复,但最后我希望这个小答案可以帮助某人..像我这样的新手...这就是我在 ggplot 部分所做的。
ggplot(DF) +
geom_line(aes(x=YEAR, y=((Rendi+5)/2.5)-5)) +
geom_bar(aes(x=YEAR, y=SPEI, col=sign, fill = sign) , show.legend = F,stat = "identity", alpha = 0.2) +
geom_label(data = DF, aes(x = YEAR, y = ((Rendi+5)/2.5)-5, label = sprintf('%.1f', Rendi)),
vjust = 0.8, size = 1.3, alpha = 0.7, position = "dodge") +
scale_color_manual(values = c("Rendimiento" = "green", "pos" = "darkblue", "neg" = "red")) +
scale_fill_manual(values = c("Rendimiento" = "green" ,"pos" = "darkblue", "neg" = "red")) +
scale_y_continuous(
name = "SPEI\n", sec.axis = sec_axis(~ ((.+5)*2.5)-5, name = "Rendimiento Papa [tn/ha]\n"),
limits = c(-5, 5)
)
我明白了
SPI index join with potato Yield
不客气。
我想用我的 SPI 函数和马铃薯产量创建一个双图,以查看它们之间的关系。我只是试试这个,但是 "y" 值不适合彼此
产量数据在这里 <- enter link description here
SPI_data 这里 <- enter link description here
library(ggplot2)
library(tidyverse)
anhos <- paste(c(1965:2018), "12", "01", sep = "-")
reemp <- paste(c(1965:2018), "01", "01", sep = "-")
yield_dt <- fread(rendi_MINAGRI.txt, header = T)
spei <- fread("ho_000253.txt", header = T)
spei <- spei[spei$YEAR %in% anhos]
spei$YEAR <- as.data.table(reemp)
spei$YEAR <- as.Date(spei$YEAR)
DF <- spei %>% dplyr::mutate(sign = ifelse(SPEI >= 0, "pos", "neg"))
DF$Rendi <- dt_rendi[,c("ANCASH")]/1000
pl <- ggplot(data = DF, mapping = aes(x = YEAR, y = Rendi, group = 1)) +
geom_line(aes(color="Rendimiento")) +
geom_point(aes(color="Rendimiento"))
pl <- pl + geom_bar(aes(y = SPEI, col = sign, fill = sign), show.legend = F, stat = "identity")+
scale_color_manual(values = c("Rendimiento" = "green", "pos" = "darkblue", "neg" ="red"))+
scale_fill_manual(values = c("Rendimiento" = "green" ,"pos" = "darkblue", "neg" = "red"))
pl <- pl + scale_y_continuous("Rendimiento Papa [tn/ha]",sec.axis = sec_axis(~., name =
toupper(indx))) +
scale_x_date(date_breaks = "2 years", date_labels = "%Y ", expand = c(0,0))
png(filename = paste0("SPEI",".png"), width = 2800, height = 1200, units = "px", pointsize = 9,res = 250 )
print(pl)
最后我有这样的东西 enter link description here,正如你所看到的,我需要一些适合两个轴的东西来比较,我不需要左右轴上的相同标签。我希望有人能帮助我。
谢谢。
感谢您没有回复,但最后我希望这个小答案可以帮助某人..像我这样的新手...这就是我在 ggplot 部分所做的。
ggplot(DF) +
geom_line(aes(x=YEAR, y=((Rendi+5)/2.5)-5)) +
geom_bar(aes(x=YEAR, y=SPEI, col=sign, fill = sign) , show.legend = F,stat = "identity", alpha = 0.2) +
geom_label(data = DF, aes(x = YEAR, y = ((Rendi+5)/2.5)-5, label = sprintf('%.1f', Rendi)),
vjust = 0.8, size = 1.3, alpha = 0.7, position = "dodge") +
scale_color_manual(values = c("Rendimiento" = "green", "pos" = "darkblue", "neg" = "red")) +
scale_fill_manual(values = c("Rendimiento" = "green" ,"pos" = "darkblue", "neg" = "red")) +
scale_y_continuous(
name = "SPEI\n", sec.axis = sec_axis(~ ((.+5)*2.5)-5, name = "Rendimiento Papa [tn/ha]\n"),
limits = c(-5, 5)
)
我明白了
SPI index join with potato Yield
不客气。