scale_x_continuous变量标签

时间:2018-04-11 19:28:20

标签: r ggplot2 shiny

我正在处理一个闪亮的应用程序,该应用程序将在x轴上绘制可变数量的因子。这些X值以年月形式出现(例如2018年4月= 201804年),因此为了绘制彼此相邻的不同年份的年月值,我将它们分配给#34; ID"。使用x_scale_discrete函数,我能够按照我喜欢的方式绘制这些值。但是,xlabels没有正确显示。请看下面的

dat1 = as.data.frame(matrix(c(201711,201712,201801,201802, 1,2,3,4, 
84,92,85,91), nrow = 4, ncol = 3, byrow = FALSE))
colnames(dat1) = c("yrmonth", "Id", "yVal")


plot1 = ggplot(data = dat1, aes(x = Id)) +
        geom_line(aes(y = yVal), size = .85) 
        #scale_x_discrete(breaks = 1:nrow(dat1), labels = c("1" = "201711", "2" = "201712", "3" = "201801", "4" = "201802")

1 个答案:

答案 0 :(得分:1)

你可以尝试

ggplot(data = dat1, aes(x = Id, y = yVal)) +
  geom_line(size = .85) +
  scale_x_continuous(labels = dat1$yrmonth)

enter image description here

但是这不会显示时间点之间的正确距离。