ggplot2显示不在数据中的列

时间:2018-07-06 15:59:22

标签: r ggplot2

我在使用ggplot时遇到问题,我认为很简单,但是以某种方式显示了不存在的数据,我如何摆脱显示x = 2和5的列的空白空间

Data <- structure(list(DQRNiveau = c(0, 1, 3, 4, 6, 7, 8, 0, 1, 3, 4, 6, 7, 8),
              Group = c("Alpha", "Alpha", "Alpha", "Alpha", "Alpha", "Alpha", "Alpha", "Beta", "Beta", "Beta", "Beta", "Beta", "Beta", "Beta"),
              n_Count = c(2L, 4L, 11L, 77L, 45L, 102L, 13L, 2L, 4L, 16L, 103L, 58L, 109L, 13L)),
              .Names = c("DQRNiveau", "Group", "n_Count"), row.names = c(NA, -14L),
              class = c("tbl_df", "tbl", "data.frame"))


ggplot(data = Data, aes(DQRNiveau, y = n_Count, fill = Group)) +
geom_bar(stat = "identity", position = position_dodge(1))  +
scale_x_continuous(breaks = c(Data$DQRNiveau)) 

enter image description here

1 个答案:

答案 0 :(得分:0)

由于DQRNiveau是数字,因此会产生空格。如果将其转换为字符,则应该可以正常工作,因为它将其视为分类变量。

ggplot(data = Data, aes(as.character(DQRNiveau), y = n_Count, fill = Group)) +
  geom_bar(stat = "identity", position = position_dodge(1)) + 
  xlab('DQRNiveau')

enter image description here