微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 ggplot2 在不同方面为 2x2 排列添加水平线?

如何解决如何使用 ggplot2 在不同方面为 2x2 排列添加水平线?

我有一个按面绘制和分隔的数据库。第一行(行 a)的刻面需要 0.5 处的水平线,而第二行(行 b)的刻面需要 1 处的线。我已经部分实现了以下目标this example。然而,0.5 和 1 处的水平线出现在所有方面。

library(ggplot2)

#Data
values <- c(0.4,0.6,0.9,1.1)
Column <- c("UW","LW","UW","LW")
Row <- c("a","a","b","b")
DF <- data.frame(Row,Column,values)
DF$Column <- factor(DF$Column,levels = c("UW","LW"))
DF$Row <- factor(DF$Row,levels = c("a","b"))

#Auxiliar DF
Target <- c("a","b")
Lines <- c(0.5,1)
Lines_in_plot <- data.frame(Target,Lines)
Lines_in_plot$Target <- factor(Lines_in_plot$Target)

#Plot
ggplot(data = DF,aes(y = values)) +
  geom_bar() +
  facet_grid(Row~Column,scales = "free") +
  geom_hline(data = Lines_in_plot,yintercept = Lines,linetype = "dashed",color = "red")

此 MWE 运行但显示以下警告消息:

geom_hline(): Ignoring `data` because `yintercept` was provided.

enter image description here

解决方法

要在特定面板中显示截取,您需要将 Row 中引用的 facet_grid 用作 Lines_in_plot 中的变量。您还需要将 yintercept 放在 aes 中,以便 ggplot 知道要引用该 Lines_in_plotyintercept 数据。

...
#Auxiliar DF
Row <- c("a","b")
Lines <- c(0.5,1)
Lines_in_plot <- data.frame(Row,Lines)
Lines_in_plot$Row <- factor(Lines_in_plot$Target)

#Plot
ggplot(data = DF,aes(y = values)) +
  geom_bar() +
  facet_grid(Row~Column,scales = "free") +
  geom_hline(data = Lines_in_plot,aes(yintercept = Lines),linetype = "dashed",color = "red")

enter image description here

,

这是您的解决方案:

filepath = './sdfdd/sdfdsst/xxxxxxxxxrrrarara.json'
with open(filepath) as fh:
mydata = fh.read()
response = requests.put('https://asdfs.sdf.sdfds.com',data=mydata,auth=('Authorization','Api-Token dsdfdsfsdfsdf'),headers={'content-type':'application/json'},params={'file': filepath},allow_redirects=True
)
print(response)

两个变化:

  1. 将 y 轴截距移动到美学中
  2. 将目标重命名为 Row 以匹配 Facet,以便它知道如何处理它们

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。