ggplot2条形图中的标签NA

时间:2019-03-14 10:15:48

标签: r ggplot2

我有一个要为其创建柱形图的数据框。

              Area IMD_Score
1  East Devon 005A        NA
2       East Devon        NA
3            Devon  65.00236
4               SW  60.28269
5          England  57.61097
6         IMD - Q3  56.90886
7 Eastern Locality  55.07258

xAxisTitle <- "IMD Score"
chartTitle <- "Overall IMD score 2015"

  latestChart = ggplot(dataset, aes_string(y=colnames(dataset[2]), x=colnames(dataset[1]) ) ) + 
    geom_col(fill="#1f78b4") + 
    coord_flip() +
    scale_x_discrete(dataset$Area) + 
    labs(y=xAxisTitle, title=chartTitle) +
    theme_bw() +
    theme(panel.background = element_rect(fill = NA),panel.ontop = TRUE,
          panel.grid = element_line(colour = "grey", size=0.25, linetype = "dashed"),
          axis.title.y=element_blank()
        ) +
    geom_label(label=dataset$IMD_Score, na.rm=FALSE)

添加数据标签给我带来两个问题: 1使用geom_text(label=dataset$IMD_Score, na.rm=FALSE)显示标签,但理想情况下,我希望它们位于条形图内。我尝试添加position = position_stack(vjust = 1),但是导致出现“错误:美学必须为长度1或与数据(5):标签相同”,我猜测这与“警告消息1:删除了包含缺失值的2行(position_stack)。“

2对于具有NA的行,我想添加一个标签以表明该值为NA而不是零。

最终我想以链接的图像结尾。

示例图表

enter image description here

任何想法表示赞赏。

dput格式的数据。

dataset <-
structure(list(Area = structure(c(3L, 2L, 1L, 7L, 
5L, 6L, 4L), .Label = c("Devon", "East Devon", 
"East Devon 005A", "Eastern Locality", "England", 
"IMD - Q3", "SW"), class = "factor"), IMD_Score = 
c(NA, NA, 65.00236, 60.28269, 57.61097, 56.90886, 
55.07258)), class = "data.frame", row.names = 
c("1", "2", "3", "4", "5", "6", "7"))

2 个答案:

答案 0 :(得分:1)

尝试一下:



library(ggplot2)
#reproduce your dataset
dataset <- data.frame(
  Area = c(
    "East Devon 005A",
    "East Devon",
    "Devon",
    "SW",
    "England",
    "IMD - Q3",
    "Eastern Locality"
  ),
  IMD_Score = c(NA,
                NA,
                65.00236,
                60.28269,
                57.61097,
                56.90886,
                55.07258)
)
# this a trick to keep rows with NA
dataset$IMD_Score_to_plot <- if_else(is.na(dataset$IMD_Score),
                                     0,
                                     dataset$IMD_Score)
#create the label with No data available
dataset$IMD_Score_label <- if_else(dataset$IMD_Score_to_plot == 0,
                                   "No data available",
                                   "")
#plot (the clue is using two geom_text)
latestChart = ggplot(dataset, aes_string(y = colnames(dataset[3]), x = colnames(dataset[1]))) +
  geom_col(fill = "#1f78b4") +
  coord_flip() +
  scale_x_discrete(dataset$Area) +
  labs(y = "xAxisTitle", title = "chartTitle") +
  theme_bw() +
  theme(
    panel.background = element_rect(fill = NA),
    panel.ontop = TRUE,
    panel.grid = element_line(
      colour = "grey",
      size = 0.25,
      linetype = "dashed"
    ),
    axis.title.y = element_blank()
  ) +
  geom_text(
    label = dataset$IMD_Score_to_plot,
    na.rm = FALSE,
    vjust = 0.5,
    hjust = 5
  ) +
  geom_text(label = dataset$IMD_Score_label,
            vjust = 0.5,
            hjust = 0)
latestChart

这是输出,我想这就是您想要的:enter image description here

答案 1 :(得分:0)

稍微修改了您的代码。主要是添加两个geom_text和删除geom_label

ggplot(dataset, aes(y = IMD_Score, x = Area)) + 
  geom_col(fill="#1f78b4") + 
  coord_flip() +
  scale_x_discrete() + 
  labs(y=xAxisTitle, title=chartTitle) +
  geom_text(aes(label= round(IMD_Score, 2), y = IMD_Score - 3), col = "white", size = 4) +
  geom_text(aes(label = ifelse(is.na(IMD_Score), "No data available", ""), y = 3), col = "gray", hjust = 0) +
  theme_bw() +
  theme(
    panel.background = element_rect(fill = NA),panel.ontop = TRUE,
    panel.grid = element_line(colour = "grey", size=0.25, linetype = "dashed"),
    axis.title.y=element_blank()
  )

enter image description here