【问题标题】:R rescale 2nd Y-axis according to a variableR根据变量重新调整第二个Y轴
【发布时间】:2020-05-21 14:46:46
【问题描述】:

我一直在尝试将直方图和折线图合并到一个图中。直方图是抗生素处方的确切数量(左 y 轴)。折线图是抗生素使用量的百分比(右轴)。到目前为止,我已经将它们合并在一起,将右轴向右移动,但右 y 轴使用与左相同的比例。直方图和折线图的尺度当然是很不一样的。

我的问题开始之前的一些信息:

  • 数据框名称 = Grafiek3
  • n_fosfo = 许可数量(已使用 对于左 y 轴)
  • Jaren = 年,金额和百分比是 每年可见(用于 x 轴)
  • per_fos = 百分比(用于 右 y 轴),大约 11%

我现在的代码是:

  ggplot(Grafiek3, aes(x = Jaren, y =n_fosfo)) + #samenvoegen
geom_bar(stat="identity", fill="#69b3a2" ) +
scale_x_continuous(labels = as.character(Grafiek3$Jaren), breaks = Grafiek3$Jaren) +
xlab ("Year") +
theme(plot.title = element_text(hjust = 0.5)) +
ggtitle("Increase fosfomycin prescriptions per year")+
geom_line(aes(x=Jaren, y=per_fos), color="black") +
geom_point(aes(x=Jaren, y=per_fos), shape=21, color="black", fill="#69b3a2", size=2) +
scale_y_continuous(
  name = expression("Total amount of prescriptions"), 
  sec.axis = sec_axis(~., name = "Percentage")) +
theme(plot.title = element_text(hjust = 0.5)) +
ggtitle("Percentage of fosfomycin prescriptions per year")

输出如下: Unfinished graph

如图所示,折线图位于底部,但应如下所示:

  ggplot(Grafiek3, aes(x=Jaren, y =per_fos)) + #line graph
  geom_line(color="grey") +
  geom_point(shape=21, color="black", fill="#69b3a2", size=2) +
  scale_x_continuous(labels = as.character(Grafiek3$Jaren), breaks = Grafiek3$Jaren) +
  scale_y_continuous(position = "right") +
  xlab ("Year") +
  ylab ("Percentage") +
  theme(plot.title = element_text(hjust = 0.5)) +
  ggtitle("Percentage of fosfomycin prescriptions per year")

图片: Line graph

我已经看到更改代码:

sec.axis = sec_axis(~., name = "Percentage"))

变成这样:

sec.axis = sec_axis(~./42, name = "Percentage"))

会修复它,但不幸的是,这只改变了右轴,但折线图不会随之移动。

那么,有人知道如何将折线图(图片)移动到直方图中吗?或者如何改变右轴,使其使用“per_fos”变量的信息?

可以使用的虚构数据集:

years <- c(2013, 2014, 2015, 2016, 2017, 2018, 2019)
amount <- c(120, 150, 200, 170, 180, 240, 80)
percentage <- c(5.4, 5.9, 6.3, 7.1, 7.8, 8.4, 10.4)
df <- data.frame(years, amount, percentage)

【问题讨论】:

  • 欢迎来到 SO。您能否使您的问题可重现:以对象的形式包含一个最小数据集,例如,如果数据框为 df minimal reproducible example 和 How to Ask
  • 很遗憾,由于隐私原因,我无法使用真实的数据集。我将尝试建立一个“组成”的数据集。预期的答案已经以我提供的 2 张图片的形式呈现,并描述了它应该如何。预期答案的真实图片当然是不可能的,否则我会问我的问题。我用过的代码也展示出来了。
  • 那太好了,通常最好使用一组简化的数据来解决问题。
  • 感谢您改进我的问题!

标签: r yaxis rescale


【解决方案1】:

只是为了让您知道,两个不同比例的 y 轴不被认为是好的做法。这就是为什么用 ggplot2 做你想做的事情并不容易。 有关该问题的良好讨论,请参阅此链接:ggplot with 2 y axes on each side and different scales



library(ggplot2)
library(dplyr)
library(scales)


years <- c(2013, 2014, 2015, 2016, 2017, 2018, 2019)
amount <- c(120, 150, 200, 170, 180, 240, 80)
percentage <- c(5.4, 5.9, 6.3, 7.1, 7.8, 8.4, 10.4)

df <- data.frame(years, amount, percentage)
# create a modified percentage column to fit in with the scale of the columns:

df <- 
  df %>% 
  mutate(pc_rescale = rescale(percentage, to = c(20, 200)))

# plot and add suitable labels; if you want the labels scaled evenly you need to unpick the rescaling function. 

ggplot(df, aes(years, amount)) +
  geom_col()+
  geom_line(aes(years, pc_rescale))+
  scale_y_continuous(sec.axis = sec_axis(~., labels = df$percentage, breaks = df$pc_rescale, name = "Percentage" ))

reprex package (v0.3.0) 于 2020 年 5 月 21 日创建

【讨论】:

  • 非常感谢!这对我帮助很大!现在我可以完成对应需求的图了。
  • 不幸的是,我无法用正确的比例获得正确的轴。更改此代码: scale_y_continuous(sec.axis = sec_axis(~., labels = df$percentage, breaks = df$pc_rescale, name = "Percentage" )) 到: scale_y_continuous(sec.axis = sec_axis(~/40., name = "Percentage" )) 也给出了错误的比例。我怎样才能拥有与我的折线图图片相同的比例?但是里面有数字 4 到 11 吗?因此,底部从 4 而不是 0 开始。
  • 没关系,我已经通过更改重新缩放的数量并更改中断和标签来修复它。
  • 没错。如果这回答了您的问题,通常点击接受它:stackoverflow.com/help/someone-answers
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-19
  • 2017-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-03
  • 1970-01-01
相关资源
最近更新 更多