我使用双Y轴绘制条形图和折线图。我想修改单个y轴的范围,例如折线图在条形图上方。在这种情况下,我希望Does
的范围为1-1.5,Wight
的范围为0.20-0.5,因此线条稍微有点条形图。但是,当我在limits
之后设置sec.axis
时,仅适用于次要y轴,条形图消失了。我尝试添加break
来指定范围,但是效果也不理想。我想知道是否可以单独操纵两个y轴?
同时使用双轴时,是否有可能对每个绘图都有一个标准误差(一个用于条形图,一个用于折线图)?我使用两个stat_summary
来具有2个不同的mean_se
(标准误差线),但是只绘制了一个。任何建议,不胜感激!
# dummy data
burger<- tibble(
Day = rep(1:4,2),
Dose = c(0.5,0.6,0.4,0.3,0.5,0.6,0.4,0.3),
Wight = c(0.95,0.92,0.93,0.98,0.95,0.92,0.93,0.98))
# plot
ggplot(data= burger, aes(x = Day, y = Dose),group = 1)+
geom_bar(data= burger, aes(x = Day, y = Wight* (0.98/0.6)), stat = "identity") +
stat_summary(fun.data = mean_se,geom= "errorbar",width=0.6,size=1,aes(y= burger$Dose, width=0.05))+
stat_summary(fun.data = mean_se,geom= "errorbar",width=0.6,size=1,aes(y= burger$Wight, width=0.05))+
geom_line (size = 2,aes(group=1))+
geom_point(aes(colour=Day)) +
scale_y_continuous(name = "Dose",
sec.axis = sec_axis(trans = ~.*(0.6/0.98) , name = "Wight",limits=c(0.25,1)))
答案 0 :(得分:0)
我添加了+1和-1来将条形图设置在线条的相同高度,同样使用coord_cartesian,您可以放大图表而无需裁剪数据。 (如果您使用ylim,则条形图会因为您的情况而消失)。 我也调整了您的代码。
require(dplyr)
require(ggplot2)
# dummy data
burger <- tibble(Day = 1:4,
Dose = c(0.5, 0.6, 0.4, 0.3),
Wight = c(0.95, 0.92, 0.93, 0.98))
# plot
ggplot(data = burger, aes(x = Day, y = Dose))+
geom_bar(aes(y = Wight * (0.98/0.6) - 1), stat = "identity") +
geom_line(size = 2) +
geom_point(aes(colour = Day)) +
scale_y_continuous(name = "Dose",
sec.axis = sec_axis(trans = ~(.+1)*(0.6/0.98),
name = "Wight")) +
coord_cartesian(ylim = c(0.3, 0.6))
编辑:
此图回答了问题的第二部分。
首先,我创建了一个随机数据样本。
使用stat_summary
创建图的所有对象。使用stat_summary
可以将摘要函数应用于数据:使用fun.y
表示平均值,当我想要从Y变量中提取单个结果时,并且需要使用mean_se
提取fun.data
时,需要返回可以由geom错误栏使用的向量。
我添加了一些颜色来帮助您识别每块,并直观地将每个值与其错误栏相关联,但是您可以设置最喜欢的颜色。
require(dplyr)
require(ggplot2)
set.seed(20)
# dummy data
burger<- tibble(
Day = rep(1:4, each = 10),
Dose = rnorm(40, 0.5, 0.1),
Wight = rnorm(40, 0.9, 0.1))
# plot
ggplot(data = burger, aes(x = Day))+
# bars and error
stat_summary(aes(y = Wight * (0.98/0.6) - 1),
fun.y = mean, geom = "bar",
fill = "steelblue", alpha = 0.6) +
stat_summary(aes(y = Wight * (0.98/0.6) - 1),
fun.data = mean_se, geom = "errorbar",
colour = "steelblue", size = 1.5, width = 0.5) +
# line/point and error
stat_summary(aes(y = Dose), fun.data = mean_se,
geom = "errorbar", size = 1.5, width = 0.5,
colour = "coral") +
stat_summary(aes(y = Dose), fun.y = mean, geom = "line",
size = 0.5, colour = "coral") +
stat_summary(aes(y = Dose, colour = Day), fun.y = mean, geom = "point", size = 3) +
# prettier
scale_y_continuous(name = "Dose",
sec.axis = sec_axis(trans = ~(.+1)*(0.6/0.98),
name = "Wight")) +
coord_cartesian(ylim = c(0.3, 0.6)) +
theme_light() +
scale_color_gradient2(mid = "black", high = "#FFBE5A")
答案 1 :(得分:0)
在ggplot2
中,您可以控制主要的y轴,并变换辅助的y轴。 plotly
允许您分别控制两个轴。
library(plotly)
plot_ly(data = burger) %>%
add_lines(x = ~Day, y = ~Dose, name = "Dose") %>%
add_bars(x = ~Day, y = ~Wight, yaxis = "y2", name = "Wight") %>%
layout(
yaxis = list(showline = TRUE, side = "left",
title = "Dose", range = c(0.2, 0.6)),
yaxis2 = list(showline = TRUE, side = "right",
overlaying = "y", title = "Wight", range = c(0.9, 1)),
showlegend = FALSE,
margin = list(pad = 0, b = 50, l = 50, r = 50))