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

标签 r ggplot2

我有一个功能 plotmonthly2使用包 ggplot2以图形方式显示我的数据每月的发展情况。

每月情节2:

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")

无论我仔细检查我的函数多少次都没有关系,我无法找到未显示轴的原因。有人可以看到问题出在哪里吗?

最佳答案

我发现由于这条线,轴消失了:

axis.text.y = element_blank(),

一般来说,element_blank()争论会抹去一些本来可能存在的东西:

Theme element: blank. This theme element draws nothing, and assigns no space



没有它它的工作原理:
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

关于R - ggplot2 - X 轴标签不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38040942/

相关文章:

R:对所有值使用 group_by

r - ggplot 中虚拟变量的分位数回归误差

r - 如何在四开本中将字幕文本居中?

R:如何使用 rCharts 绘制统计函数

r - 如何在ggplot2绘图区域上获得一种以上的背景颜色?

r - 如何在 ggplot2 中使用点表示第一个数据框并使用线表示它们之间的变化来绘制两个数据框?

r - 在饼图上放置标签 ggplot2

r - 为组合图添加一个包含所有变量的图例

r - 在bookdown首页之前插入pdf

c - 在R中调用C时是否有可访问的 vector 接口(interface)函数