gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:05:14 26 4
gpt4 key购买 nike

我有一个功能 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com