R - ggplot2 - X轴标签不会显示

时间:2016-06-26 17:00:25

标签: r ggplot2

我有一个函数plotmonthly2,它使用包ggplot2来图形显示我的数据的每月发展。

plotmonthly2:

plotmonthly2 <- function (my_data) {

  ymin <- 0
  ymax <- max(my_data[my_data[6] == unique(my_data[,6])[1], 4])+max(my_data[my_data[6] == unique(my_data[,6])[2], 4])

  ggplot(my_data, aes( Cat, value)) +
    geom_area(aes(fill= type), position = 'stack') +
    scale_y_continuous(expand = c(0,0), limits = c(ymin,ymax)) +
    scale_fill_manual(values = c("#797978", "#a6a4a1")) +
    scale_x_discrete(expand = c(0,0), labels = gsub(" 20"," ",my_data$monthYear)) +
    theme(axis.text.x = element_text(angle=90, vjust=1, face="bold", size=7, colour = "#016576"),
          axis.title = element_blank(),
          axis.text.y = element_blank(),
          axis.ticks=element_blank(),
          axis.line.x = element_line(color="black", size = 2),
          panel.background = element_rect(fill = "transparent",colour = NA),
          panel.grid.minor = element_blank(), 
          panel.grid.major = element_blank(),
          legend.position = "none",
          plot.background = element_rect(fill = "transparent",colour = NA)) 
}

但是,当我绘制数据框时,不会显示x轴标签:

pmKM <- plotmonthly2(monthlyKMDef)

Plot without x axis labels

这是我的数据框的结构:

> dput(monthlyKMDef)
monthlyKMDef <- structure(list(month = c("Mai", "Jun", "Jul", "Aug", "Sep", "Okt", 
"Nov", "Dez", "Jan", "Feb", "Mrz", "Apr", "Mai", "Mai", "Jun", 
"Jul", "Aug", "Sep", "Okt", "Nov", "Dez", "Jan", "Feb", "Mrz", 
"Apr", "Mai"), year = c("2015", "2015", "2015", "2015", "2015", 
"2015", "2015", "2015", "2016", "2016", "2016", "2016", "2016", 
"2015", "2015", "2015", "2015", "2015", "2015", "2015", "2015", 
"2016", "2016", "2016", "2016", "2016"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Count", class = "factor"), 
    value = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 134, 166, 191, 237, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 43, 49, 90, 173), monthYear = structure(c(2015.33333333333, 
    2015.41666666667, 2015.5, 2015.58333333333, 2015.66666666667, 
    2015.75, 2015.83333333333, 2015.91666666667, 2016, 2016.08333333333, 
    2016.16666666667, 2016.25, 2016.33333333333, 2015.33333333333, 
    2015.41666666667, 2015.5, 2015.58333333333, 2015.66666666667, 
    2015.75, 2015.83333333333, 2015.91666666667, 2016, 2016.08333333333, 
    2016.16666666667, 2016.25, 2016.33333333333), class = "yearmon"), 
    type = c("Eigene", "Eigene", "Eigene", "Eigene", "Eigene", 
    "Eigene", "Eigene", "Eigene", "Eigene", "Eigene", "Eigene", 
    "Eigene", "Eigene", "Mentions", "Mentions", "Mentions", "Mentions", 
    "Mentions", "Mentions", "Mentions", "Mentions", "Mentions", 
    "Mentions", "Mentions", "Mentions", "Mentions"), Cat = c(1L, 
    2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 
    3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L)), .Names = c("month", 
"year", "variable", "value", "monthYear", "type", "Cat"), row.names = c(NA, 
-26L), class = "data.frame")

无论我多次检查我的功能多少次,我都无法找到未显示轴的原因。有人能看出问题所在吗?

1 个答案:

答案 0 :(得分:2)

我发现轴因此而消失:

axis.text.y = element_blank(),

通常,element_blank()参数会删除一些可能存在的东西:

  

主题元素:空白。这个主题元素什么都没画,并指定   没有空间

没有它就可以了:

plotmonthly2 <- function (my_data) {

  ymin <- 0
  ymax <- max(my_data[my_data[6] == unique(my_data[,6])[1], 4])+max(my_data[my_data[6] == unique(my_data[,6])[2], 4])

  ggplot(my_data, aes( Cat, value)) +
    geom_area(aes(fill= type), position = 'stack') +
    scale_y_continuous(expand = c(0,0), limits = c(ymin,ymax)) +
    scale_fill_manual(values = c("#797978", "#a6a4a1")) +
    scale_x_discrete(expand = c(0,0), labels = gsub(" 20"," ",my_data$monthYear)) +
    theme(axis.text.x = element_text(angle=90, vjust=1, face="bold", size=7, colour = "#016576"),
#           axis.title = element_blank(),
#           axis.text.y = element_blank(),
          axis.ticks=element_blank(),
          axis.line.x = element_line(color="black", size = 2),
          panel.background = element_rect(fill = "transparent",colour = NA),
          panel.grid.minor = element_blank(), 
          panel.grid.major = element_blank(),
          legend.position = "none",
          plot.background = element_rect(fill = "transparent",colour = NA)) 
}
require(ggplot2)
plot(plotmonthly2(monthlyKMDef))

enter image description here

相关问题