如何在 ggplot R 的范围内限制次要 y 轴的比例?
How to limit the scale of secondary y-axis in a range in ggplot R?
我想将次轴固定为 0 到 1,因为概率始终在 0 到 1 之间。但是,根据数据,如果最大值 return (y) 很高(如如下图所示,6%),那么副 y 轴的刻度将超过 1(因为它取决于主 y 轴),这不是一个理想的呈现情况。我应该如何限制次要 y 轴的最大比例?
我听说ggplot
仍然不允许我们限制副轴的比例,但我不确定是否属实。是否有任何其他方法可以限制次要 y 轴的比例,以便图表上显示的最大概率始终为 1 或小于 1,具体取决于最大值 return。请注意,代码中显示的 y 是 return.
y <- c(0.01, -0.005, 0.06)
Month <- c("Jan", "Feb", "Mar")
dtf <- data.frame(Month, y)
require(reshape)
dtf2 <- melt(dtf)
dtf2[["sign"]] = ifelse(dtf2[["value"]] >= 0, "positive", "negative")
Probability <- c(0.4,0.22,0.54)
dtf2 <- data.frame(dtf2, Probability)
dtf2 %>>% ggplot() +
geom_bar(mapping = aes(x=Month, y=value, fill = sign), stat ="identity", width = 0.75)+
geom_point(mapping = aes(x=Month, y=Probability/20-0.025), size = 2.5, color="blue")+
geom_line(mapping = aes(x=Month, y=Probability/20-0.025, group = 1), color = "blue", size = 1)+
geom_hline(yintercept=0)+
geom_text(aes(x=Month, y=value, label = paste(y * 100,"%"), vjust = ifelse(y >= 0, -0.5, 1.2)), hjust = 0.5)+
ylab("\nReturn")+
theme(axis.text.x = element_text(face = "bold", size=11),
axis.text.y = element_text(face = "bold"),
axis.title.x=element_blank(),
legend.position = "none",
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5))+
theme(panel.background = element_blank(),
axis.ticks.x = element_blank()) +
theme(axis.line.y = element_line(color="black", size = 0.5))+
scale_y_continuous(labels=scales::percent, sec.axis = sec_axis(~(. + 0.025)*20, name = "Probability\n" ))+
scale_fill_manual(values = c("positive" = "black", "negative" = "red"))
我希望次要 y 轴的最大刻度始终为 1 或小于 1(如果 return 较小)。请多多指教,谢谢!
library(tidyverse)
library(rashape2)
y <- c(0.01, -0.05, 0.06)
Month <- c("Jan", "Feb", "Mar")
dtf <- data.frame(Month, y)
require(reshape)
dtf2 <- melt(dtf)
dtf2[["sign"]] = ifelse(dtf2[["value"]] >= 0, "positive", "negative")
Probability <- c(0.4,0.22,0.54)
yrange <-max(y)- min(y)
ymin <- min(y)
dtf2 <- data.frame(dtf2, Probability)
dtf2 %>% ggplot() +
geom_bar(mapping = aes(x=Month, y=value, fill = sign), stat ="identity", width = 0.75)+
geom_point(mapping = aes(x=Month, y=Probability*yrange+ymin), size = 2.5, color="blue") +
geom_line(mapping = aes(x=Month, y=Probability*yrange+ymin, group = 1), color = "blue", size = 1)+
geom_hline(yintercept=0)+
geom_text(aes(x=Month, y=value, label = paste(y * 100,"%"), vjust = ifelse(y >= 0, -0.5, 1.2)), hjust = 0.5)+
ylab("\nReturn")+
theme(axis.text.x = element_text(face = "bold", size=11), axis.text.y = element_text(face = "bold"),
axis.title.x=element_blank(),
legend.position = "none",
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5))+
theme(panel.background = element_blank(),
axis.ticks.x = element_blank()) +
theme(axis.line.y = element_line(color="black", size = 0.5))+
scale_y_continuous(labels=scales::percent, sec.axis = sec_axis(~(. -ymin)/yrange, name = "Probability\n", breaks = c(0,0.5,1)))+
scale_fill_manual(values = c("positive" = "black", "negative" = "red"))
我想将次轴固定为 0 到 1,因为概率始终在 0 到 1 之间。但是,根据数据,如果最大值 return (y) 很高(如如下图所示,6%),那么副 y 轴的刻度将超过 1(因为它取决于主 y 轴),这不是一个理想的呈现情况。我应该如何限制次要 y 轴的最大比例?
我听说ggplot
仍然不允许我们限制副轴的比例,但我不确定是否属实。是否有任何其他方法可以限制次要 y 轴的比例,以便图表上显示的最大概率始终为 1 或小于 1,具体取决于最大值 return。请注意,代码中显示的 y 是 return.
y <- c(0.01, -0.005, 0.06)
Month <- c("Jan", "Feb", "Mar")
dtf <- data.frame(Month, y)
require(reshape)
dtf2 <- melt(dtf)
dtf2[["sign"]] = ifelse(dtf2[["value"]] >= 0, "positive", "negative")
Probability <- c(0.4,0.22,0.54)
dtf2 <- data.frame(dtf2, Probability)
dtf2 %>>% ggplot() +
geom_bar(mapping = aes(x=Month, y=value, fill = sign), stat ="identity", width = 0.75)+
geom_point(mapping = aes(x=Month, y=Probability/20-0.025), size = 2.5, color="blue")+
geom_line(mapping = aes(x=Month, y=Probability/20-0.025, group = 1), color = "blue", size = 1)+
geom_hline(yintercept=0)+
geom_text(aes(x=Month, y=value, label = paste(y * 100,"%"), vjust = ifelse(y >= 0, -0.5, 1.2)), hjust = 0.5)+
ylab("\nReturn")+
theme(axis.text.x = element_text(face = "bold", size=11),
axis.text.y = element_text(face = "bold"),
axis.title.x=element_blank(),
legend.position = "none",
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5))+
theme(panel.background = element_blank(),
axis.ticks.x = element_blank()) +
theme(axis.line.y = element_line(color="black", size = 0.5))+
scale_y_continuous(labels=scales::percent, sec.axis = sec_axis(~(. + 0.025)*20, name = "Probability\n" ))+
scale_fill_manual(values = c("positive" = "black", "negative" = "red"))
我希望次要 y 轴的最大刻度始终为 1 或小于 1(如果 return 较小)。请多多指教,谢谢!
library(tidyverse)
library(rashape2)
y <- c(0.01, -0.05, 0.06)
Month <- c("Jan", "Feb", "Mar")
dtf <- data.frame(Month, y)
require(reshape)
dtf2 <- melt(dtf)
dtf2[["sign"]] = ifelse(dtf2[["value"]] >= 0, "positive", "negative")
Probability <- c(0.4,0.22,0.54)
yrange <-max(y)- min(y)
ymin <- min(y)
dtf2 <- data.frame(dtf2, Probability)
dtf2 %>% ggplot() +
geom_bar(mapping = aes(x=Month, y=value, fill = sign), stat ="identity", width = 0.75)+
geom_point(mapping = aes(x=Month, y=Probability*yrange+ymin), size = 2.5, color="blue") +
geom_line(mapping = aes(x=Month, y=Probability*yrange+ymin, group = 1), color = "blue", size = 1)+
geom_hline(yintercept=0)+
geom_text(aes(x=Month, y=value, label = paste(y * 100,"%"), vjust = ifelse(y >= 0, -0.5, 1.2)), hjust = 0.5)+
ylab("\nReturn")+
theme(axis.text.x = element_text(face = "bold", size=11), axis.text.y = element_text(face = "bold"),
axis.title.x=element_blank(),
legend.position = "none",
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5))+
theme(panel.background = element_blank(),
axis.ticks.x = element_blank()) +
theme(axis.line.y = element_line(color="black", size = 0.5))+
scale_y_continuous(labels=scales::percent, sec.axis = sec_axis(~(. -ymin)/yrange, name = "Probability\n", breaks = c(0,0.5,1)))+
scale_fill_manual(values = c("positive" = "black", "negative" = "red"))