【问题标题】:R ggplot2 x-axis not properly showR ggplot2 x轴未正确显示
【发布时间】:2017-03-17 06:03:08
【问题描述】:

我的问题如下。
这是我的原始数据:

type,com,year,month,value
A,CH,2016,1,0
A,CH,2016,2,0
A,CH,2016,3,0
A,CH,2016,4,0
A,CH,2016,5,0
A,CH,2016,6,0
A,CH,2016,7,0
A,CH,2016,8,0
A,CH,2016,9,453128
A,CH,2016,10,868256
A,CH,2016,11,1015080
A,CH,2016,12,650912
B,CH,2016,1,0
B,CH,2016,2,0
B,CH,2016,3,0
B,CH,2016,4,0
B,CH,2016,5,0
B,CH,2016,6,61273
B,CH,2016,7,27711
B,CH,2016,8,161780
B,CH,2016,9,48889
B,CH,2016,10,72805
B,CH,2016,11,131466
B,CH,2016,12,73756
C,CH,2016,1,0
C,CH,2016,2,0
C,CH,2016,3,0
C,CH,2016,4,0
C,CH,2016,5,0
C,CH,2016,6,0
C,CH,2016,7,0
C,CH,2016,8,2200
C,CH,2016,9,111384
C,CH,2016,10,28758
C,CH,2016,11,21161
C,CH,2016,12,0

我用它用 gglot2 绘制折线图。

代码是:

test <- read.csv("test.csv", header = T)
test_list <- split(test, test$type)
Rplot <- ggplot(test_list$A, aes(x=month, y=value, col=com))+geom_line()
Rplot

我的问题:
如何让我的 x 轴显示为 month(1,2,3....,12)?
以及如何将 yearmonth 同时显示在 x 轴上。(Jan2016, Feb2016,.....) 或 (2016/1, 2016 /2,.....)

非常感谢。

【问题讨论】:

    标签: r ggplot2 axis axis-labels


    【解决方案1】:

    使用来自lubridate 包的ymd() 在数据框中创建date_label 列作为Date 类。

    library( 'lubridate' )
    test_list <- lapply( test_list, function( x ) { 
      x$date_label <- ymd( paste( x$year, month.abb[ x$month ], 01, sep = "-"))
      x 
      })
    

    使用scale_x_date()操纵x轴

    # 1. date_labels = "%Y/%m"   - 2016/01
    # 2. date_labels = "%m"      - 01
    # 3. date_labels = "%b %Y"   - Jan 2016        
    
    library( 'ggplot2' )
    ggplot( data = test_list$A, aes( x = date_label, y = value, color = com, group = com ) ) +
      geom_line( size = 3 ) + 
      scale_x_date(name = "month",
                   date_labels = "%m", 
                   date_breaks = "1 month" )
    

    ggplot( data = test_list$A, aes( x = date_label, y = value, group = com, color = com ) ) +
      geom_line(  size = 3 ) + 
      theme( axis.text.x = element_text( angle = 45, hjust = 1 )) +
      scale_x_date(name = "month_year",
                   date_labels = "%b %Y", 
                   date_breaks = "1 month" )
    

    【讨论】:

    • 谢谢。如何调整绘图的宽度和高度?因为如果我的数据有很多行,x 轴会变得太拥挤。
    • 你在说固定纵横比吗
    • 图形宽度和高度:取决于用于创建图形的图形设备。有关图形设备的列表,请参阅capabilities()。要创建 jpeg 图像,您将使用 jpeg() 设备。对于 pdf,它将是 pdf()
    • 哦。对不起我的模棱两可的描述。我的意思是我的情节的x轴有太多的值(年+月),如何让它们明确而正确地显示。
    【解决方案2】:

    对于第一种情况,您可以将月份转换为一个因素。以下代码将消除月份中的小数。

    test$month <- as.factor(test$month)
    test_list <- split(test, test$type)
    Rplot <- ggplot(test_list$A, aes(x=month, y=value, col=com, group = 1))+geom_line()
    Rplot
    

    要获得数字月份,您必须有一个新列。我将使用dplyr 包创建一个新的并更改它。

    library(dplyr)
    # make a new column by joining month after converting it to character and year.
    test <- test %>% mutate(exactDate = paste(month.abb[as.numeric(as.character(month))], year, sep=""))
    
    # convert to factor
    test$exactDate <- factor(test$exactDate, levels = c("Jan2016","Feb2016","Mar2016","Apr2016","May2016","Jun2016",
                                                        "Jul2016","Aug2016","Sep2016", "Oct2016", "Nov2016", "Dec2016"),
                             labels =  c("Jan2016","Feb2016","Mar2016","Apr2016","May2016","Jun2016",
                                        "Jul2016","Aug2016","Sep2016", "Oct2016", "Nov2016", "Dec2016"))
    
    # Plot similarly
    test_list <- split(test, test$type)
    Rplot <- ggplot(test_list$A, aes(x=exactDate, y=value, col=com, group = 1))+geom_line()
    
    # Rotate axis labels by 90 degrees
    Rplot+ theme(axis.text.x=element_text(angle=90, hjust=1))
    

    【讨论】:

    • 我收到此错误:eval 中的错误(expr、envir、enclos):找不到对象“exactDate”
    • test &lt;- test %&gt;% mutate(exactDate = paste(month.abb[as.numeric(as.character(month))], year, sep="")) 应该添加一个名为exactDate 的列,并且应该不会发生此错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多