将标签添加到散点图

时间:2020-06-23 18:19:44

标签: r ggplot2 label annotate geom

我似乎无法在散点图中添加标签。

我想用变量states标记散点图。由于与annotate或某些其他geom函数的冲突,当前无法正常工作。

这是我的数据:

datatrials <- structure(list(states = c(
  "AP", "AR", "AS", "BR", "CH", "GJ",
  "HR", "HP", "JK", "JH", "KA", "KL", "MP", "NL", "OR", "PY", "PB",
  "RJ", "SK", "TG", "TR", "UP", "UT"
), cured = c(
  60.44117647, 2.631578947,
  24.70095694, 47.31207289, 71.09634551, 67.46961326, 36.86526743,
  41.78272981, 35.24676234, 42.68617021, 37.26310608, 43.57429719,
  63.40242198, 0, 59.29648241, 30.48780488, 85.3956229, 69.87152922,
  0, 51.52317881, 36.96581197, 25.99078341, 59.29659755
), Total = c(
  4080L,
  38L, 1672L, 4390L, 301L, 18100L, 2954L, 359L, 2857L, 752L, 4063L,
  1494L, 8588L, 58L, 2388L, 82L, 2376L, 9652L, 2L, 3020L, 468L,
  1085L, 8729L
), deaths = c(
  1.666666667, 0, 0.23923445, 0.569476082,
  1.661129568, 6.198895028, 0.778605281, 1.39275766, 1.190059503,
  0.664893617, 1.304454836, 0.736278447, 4.319981369, 0, 0.293132328,
  0, 1.978114478, 2.165354331, 0, 3.278145695, 0, 0.737327189,
  2.623439111
), SLT_preval = c(
  7.1, 39.3, 41.7, 23.5, 6.1, 19.2,
  6.3, 3.1, 4.3, 35.4, 16.3, 5.4, 28.1, 39, 42.9, 4.7, 8, 14.1,
  9.7, 10.1, 48.5, 29.4, 12.4
), smoking_preval = c(
  14.2, 22.7,
  13.3, 5.1, 9.4, 7.7, 19.7, 14.2, 20.8, 11.1, 8.8, 9.3, 10.2,
  13.2, 7, 7.2, 7.3, 13.2, 10.9, 8.3, 27.7, 13.5, 18.1
), density_Population = c(
  330.7605972,
  18.75330475, 453.9513884, 1325.360556, 10162.04386, 325.839688,
  637.9420067, 133.8522264, 108.3866651, 484.1552049, 352.2724528,
  918.5972004, 276.9192201, 135.6954581, 297.7151573, 2951.02714,
  598.4943608, 236.7722235, 97.27325254, 351.2115064, 397.6534427,
  987.360228, 210.3632556
), avg_tobacco_use = c(
  10.65, 31, 27.5,
  14.3, 7.75, 13.45, 13, 8.65, 12.55, 23.25, 12.55, 7.35, 19.15,
  26.1, 24.95, 5.95, 7.65, 13.65, 10.3, 9.2, 38.1, 21.45, 15.25
), urbanization = c(
  29.47, 22.94, 14.1, 11.29, 97.25, 42.6, 34.88,
  10.03, 26.11, 24.05, 38.67, 47.7, 27.63, 28.86, 16.69, 68.33,
  37.48, 24.87, 25.15, 38.88, 26.17, 22.27, 30.23
), gats2_tobacco_india = c(
  20,
  45.5, 48.2, 25.9, 13.7, 25.1, 23.6, 16.1, 23.7, 38.9, 22.8, 12.7,
  34.2, 43.3, 45.6, 11.2, 13.4, 24.7, 17.9, 17.8, 64.5, 35.5, 26.5
), Cases_per_pop = c(
  7.56909681, 2.419676298, 4.695700757, 3.517630291,
  25.98247866, 28.33774883, 10.4734347, 4.817527749, 20.99759524,
  1.948492028, 6.013674471, 4.184939244, 10.06104045, 2.578127257,
  5.151399591, 5.80103032, 7.882852583, 11.91124239, 0.289749671,
  7.672231694, 11.22357603, 0.456107101, 77.58519395
)), class = "data.frame", row.names = c(
  NA,
  -23L
))

这是我的代码:

library(ggplot2)
library(ggExtra)
library(gridExtra)

#working plot 
plot1 <- ggplot(datatrials, aes(SLT_preval,urbanization)) + geom_point(color = '#CC9933') +
  geom_smooth(fullrange=TRUE,method = "lm", level=0.95) +
  ylab("Urbanization %") +
  xlab("Smokeless Tobacco Use %") +
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=14)) +
  scale_x_continuous(expand=c(0,0), limits=c(0,100)) +
  scale_y_continuous(expand=c(0,0), limits=c(-50,100)) +
  coord_cartesian(xlim = c(0, 70), ylim = c(0, 100)) +
  theme(axis.title.y = element_text(margin=margin (t=0, r=5, b=0, l=0))) +
  geom_label(x = 0.95*max(SLT_preval), y = 0.92*max(urbanization), size = 4.3, label = "n = 32; p-value = 0.015; \n CI = -0.799:-0.050; rho = -0.426")

plot2 <- ggplot(datatrials, aes(smoking_preval,urbanization)) + geom_point(color = '#615513') +
  geom_smooth(fullrange=TRUE,method = "lm", se=FALSE) +
  ylab("Urbanization %") +
  xlab("Smoking %") +
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=14)) +
  scale_x_continuous(expand=c(0,0), limits=c(0,100)) +
  scale_y_continuous(expand=c(0,0), limits=c(-50,100)) +
  coord_cartesian(xlim = c(0, 70), ylim = c(0, 100)) +
  geom_label(x = 1.35*max(smoking_preval), y = 0.92*max(urbanization), size = 4.3, label = "n = 32; p-value = 0.186; \n CI = -0.641:0.165; rho = -0.239")

p1 <- ggMarginal(plot1, type="histogram", colour = '#FF0000', fill = '#FAC95F')
p2 <- ggMarginal(plot2, type="histogram", colour = '#FF0000', fill = '#615513')
grid.arrange(p1, p2, ncol=2)

1 个答案:

答案 0 :(得分:0)

尝试一下。我添加了geom_text。下一个。您使用的geom_label出错,缺少datatrials$。另外,如果您添加geom = "label",我也改用了注释。

library(ggplot2)
library(ggExtra)

# working plot
plot1 <- ggplot(datatrials, aes(SLT_preval, urbanization)) +
  geom_point(color = "#CC9933") +
  geom_smooth(fullrange = TRUE, method = "lm", level = 0.95) +
  # Add geom_text
  geom_text(aes(label = states)) +
  ylab("Urbanization %") +
  xlab("Smokeless Tobacco Use %") +
  theme(
    axis.text = element_text(size = 14),
    axis.title = element_text(size = 14)
  ) +
  scale_x_continuous(expand = c(0, 0), limits = c(0, 100)) +
  scale_y_continuous(expand = c(0, 0), limits = c(-50, 100)) +
  coord_cartesian(xlim = c(0, 70), ylim = c(0, 100)) +
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 5, b = 0, l = 0))) +
  # Switch to annotate. Add geom. Add datatrials$
  annotate(
    geom = "label", x = 0.95 * max(datatrials$SLT_preval),
    y = 0.92 * max(datatrials$urbanization), size = 4.3, label = "n = 32; p-value = 0.015; \n CI = -0.799:-0.050; rho = -0.426"
  )

plot2 <- ggplot(datatrials, aes(smoking_preval, urbanization)) +
  geom_point(color = "#615513") +
  geom_smooth(fullrange = TRUE, method = "lm", se = FALSE) +
  # Add geom_text
  geom_text(aes(label = states)) +
  ylab("Urbanization %") +
  xlab("Smoking %") +
  theme(
    axis.text = element_text(size = 14),
    axis.title = element_text(size = 14)
  ) +
  scale_x_continuous(expand = c(0, 0), limits = c(0, 100)) +
  scale_y_continuous(expand = c(0, 0), limits = c(-50, 100)) +
  coord_cartesian(xlim = c(0, 70), ylim = c(0, 100)) +
  # Switch to annotate. Add geom. Add datatrials$
  annotate(geom = "label", x = 1.35 * max(datatrials$smoking_preval), y = 0.92 * max(datatrials$urbanization), size = 4.3, label = "n = 32; p-value = 0.186; \n CI = -0.641:0.165; rho = -0.239")

p1 <- ggMarginal(plot1, type = "histogram", colour = "#FF0000", fill = "#FAC95F")
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'
p2 <- ggMarginal(plot2, type = "histogram", colour = "#FF0000", fill = "#615513")
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'

#par(mfrow = c(1, 2))
gridExtra::grid.arrange(p1, p2, ncol = 2)

reprex package(v0.3.0)于2020-06-23创建