【问题标题】:Label specific points in ggplot在ggplot中标记特定点
【发布时间】:2019-06-26 09:39:02
【问题描述】:

我希望通过 ggplot 创建的某些点在图表的一侧使用标签,但我无法通过我当前的代码做到这一点。

Ceplane1 是一个两列 100 行的矩阵(可以取任意随机数)。我想在x-axis 上绘制column 2y-axis 上的column 1。我已经使用下面的代码完成了这部分。现在我想对代码进行更改,以便可以将标签放在图形的一侧,而不是图形区域本身。此外,我想以逗号格式表示轴。您可以将result.table[1,1]result.table[1,3] 作为一些数字并提出解决方案。

ggplot(Ceplane1, aes(x = Ceplane1[,2], y = Ceplane1[,1])) +
  geom_point(colour="blue")+geom_abline(slope = -results.table[5,1],intercept = 0,colour="darkred",size=1.25)+
  geom_point(aes(mean(Ceplane1[,2]),mean(Ceplane1[,1])),colour="red")+
  geom_point(aes(results.table[1,1],results.table[3,1],colour="darkred"))+ggtitle("CE-Plane: Drug A vs Drug P")+
  xlab("QALY Difference")+ylab("Cost Difference")+xlim(-0.05,0.05)+ylim(-6000,6000)+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(),plot.background = element_rect(fill = "white", colour = "black", size = 0.5))+
  geom_vline(xintercept = 0,colour="black")+geom_hline(yintercept = 0,colour="black")+
  geom_label(aes(mean(Ceplane1[,2]),mean(Ceplane1[,1])),label="mean")+
  geom_label(aes(results.table[1,1],results.table[3,1]),label="Base ICER")

我想将标签放在图表的一侧,而不是放在图表本身的点上。请给我建议一种方法。

【问题讨论】:

  • 这些是几个geom 电话,您是否考虑过另一种表示?或者reshape你的数据?
  • Ceplane1是你说的矩阵,但results.table是什么?
  • 对 result.table 值取任意数字。请建议我如何为基础冰块添加标签,并在图表的一侧表示。
  • 你的意思是传说中的吗?
  • 是的,我希望标签名称出现在图例中

标签: r ggplot2


【解决方案1】:

我认为最好的方法是将meanBase ICER 点添加到您的数据集中。然后为legend 添加一列,您会看到它们在图表和图例中显示为匹配:

library(ggplot2)

set.seed(1)
Ceplane1 <- data.frame(y = rnorm(100), 
                  x = rnorm(100))
results.table <- data.frame(z = rnorm(100))
Ceplane1$Legend <- "Data"

meanPoint <- data.frame(y = mean(Ceplane1[,1]), x = mean(Ceplane1[,2]), Legend = "Mean")
basePoint <- data.frame(y = results.table[3,1], x = results.table[1,1], Legend = "Base ICER")

Ceplane1 <- rbind(Ceplane1, meanPoint)
Ceplane1 <- rbind(Ceplane1, basePoint)

ggplot(Ceplane1, aes(x = x, y = y, color = Legend)) +
  geom_point() +
  geom_abline(slope = -results.table[5,1],intercept = 0,colour="darkred",size=1.25) +
  ggtitle("CE-Plane: Drug A vs Drug P")+ xlab("QALY Difference")+ylab("Cost Difference") +
  xlim(-3,3) + ylim(-3,3) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        panel.background = element_blank(),plot.background = element_rect(fill = "white", colour = "black", size = 0.5)) +
  geom_vline(xintercept = 0,colour="black") +
  geom_hline(yintercept = 0,colour="black")

这给了我以下信息:

请注意,我更改了 xlim 和 ylim 以匹配我创建的随机数据。

【讨论】:

    猜你喜欢
    • 2021-04-09
    • 2019-08-27
    • 2015-12-14
    • 1970-01-01
    • 2019-04-26
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多