【问题标题】:Control time-series axis label and color in R stacked bar控制 R 堆叠条中的时间序列轴标签和颜色
【发布时间】:2018-06-29 11:53:38
【问题描述】:

我正在尝试绘制堆积条形图。它的工作,但仍然有一些我不明白的功能,例如,xts 做什么?我是否正在使用我加载的所有库?替换轴标签,它使用原始数据工作,但不适用于融化数据(数据被融化以产生堆叠条,因为我没有找到任何其他方法来使用 data.frame 产生堆叠条)。我还想为这个堆叠条使用单色。我尝试将 'fill = variable' 替换为 'fill = c("orange", "blue", "green")' 只是为了尝试,它不起作用。请帮忙..谢谢..

library(ggplot2)
library(xts)
library(reshape2)
library(lubridate)
library(zoo)
setwd("C:/Users/Hp/Documents/yr")
data1 <- read.csv("yr1983.csv", head = TRUE, stringsAsFactors = FALSE)
data1$Date <- dmy(data1$Date)
#data1 <- xts(x = data1[,-1], order.by = data1[,1])

head(data1)
         Date Inland middle coastal
 1 1983-11-01    0.0    0.0     0.0
 2 1983-11-02    0.0    0.0     0.0
 3 1983-11-03   90.5   19.5    60.0
 4 1983-11-04   88.5   28.5    53.8
 5 1983-11-05   80.5   73.0   122.0
 6 1983-11-06  179.5  102.0   141.3

#plot stacked bar
data.m <- melt(data1,id.vars = "Date")
p1 <- ggplot(data.m, aes(x = Date, y = value,fill=variable)) + 
geom_bar(stat='identity')
p1

#try to rename the axis - error
  Rainfall_Intensity <- data1$value
  month <- data1$Date
  ggplot(data.m, aes(x = month, y = Rainfall_Intensity,fill= variable)) + 
  geom_bar(stat='identity')
  *Error: Aesthetics must be either length 1 or the same as the data (276): x, y, fill

  ggplot(data1, aes(month, y = Rainfall_Intensity,fill= variable)) + geom_bar(stat='identity')
 *Error in eval(expr, envir, enclos) : object 'Date' not found

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    看那个:

    Rainfall_Intensity <- data1$value
    month <- data1$Date
    

    变量 Rainfall_Intensity 和月份它们不在 data.m 内。因此,当您使用 ggplot 时,它会生成上述错误。您必须重命名变量:

    rename(data.m,Rainfall_Intensity = value, month = Date)
    

    然后,运行您的 ggplot2。

    【讨论】:

    • 函数重命名找不到,需要特殊封装吗?
    • 它是 dplyr 的一个功能
    【解决方案2】:

    aes 下的fill = variable 指的是应该根据该变量来分隔堆叠的条形图。要更改堆叠条的颜色,您需要更改 geom_bar 下的填充

    ggplot(data.m, aes(x = Date, y = value,fill=variable)) 
                 + geom_bar(stat='identity', fill = c("orange", "blue", "green"))
    

    你可以参考-http://sape.inf.usi.ch/quick-reference/ggplot2/colour-选择颜色。

    【讨论】:

    • 您提供的链接应该对我有益...仍然尝试弄清楚..我尝试了您的 ggplot 然后它在 +geom_bar(stat = "identity", fill = c( "orange", "blue", "green")) : 一元运算符的参数无效
    • 我正在使用 + scale_fill_manual(values = c("thistle4", "violet", "blue")) 及其工作..谢谢您的提示..
    • 我正在使用 + scale_fill_manual(values = c("thistle4", "violet", "blue")) 及其工作..谢谢您的提示..
    【解决方案3】:

    ggplot2 对整个数据帧进行操作,因此它希望您用于映射到 aes 中的美学的任何名称都是数据帧中提供给初始 data 参数的裸列名称 @987654327 @ call 或 data 参数用于特定的几何。因此,如果您有一个名为date 的全局变量并且您调用了ggplot(data, aes(x = date, y = value)),它将在data 中查找名为date 的列,如果没有找到则会抛出错误。

    如果您需要重命名数据框中的列,您可以使用多种不同的方式,例如names(data.m) &lt;- c(...)setNames(data.m, c(...))

    但如果您只需要更改轴标签,您可以将其作为构建绘图的一部分。要么使用labs 分配标签,要么在相应的缩放函数中分配一个标签。

    使用labs 一次更改多个标签(我只是根据数据样本猜测的):

    library(tidyverse)
    
    ...
    
    ggplot(data.m, aes(x = Date, y = value, fill = variable)) +
      geom_col() +
      labs(x = "Month", y = "Rainfall intensity", fill = "Location", 
           title = "Rainfall intensity by location", 
           subtitle = "November 1983")
    

    在对 scale_x_date 的调用中仅更改 x 轴标签:

    ggplot(data.m, aes(x = Date, y = value, fill = variable)) +
      geom_col() +
      scale_x_date(name = "Month")
    

    reprex package (v0.2.0) 于 2018 年 6 月 29 日创建。

    【讨论】:

      猜你喜欢
      • 2017-01-17
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-25
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多