Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用ggplot或Plot在中绘制月度数据_R_Ggplot2 - Fatal编程技术网

R 使用ggplot或Plot在中绘制月度数据

R 使用ggplot或Plot在中绘制月度数据,r,ggplot2,R,Ggplot2,我想用x轴上的月份来绘制这个数据框 month value1 value2 value3 value4 1 Okt 19.5505 19.6145 19.5925 19.3710 2 Nov 21.8750 21.7815 21.7995 20.5445 3 Dez 25.4335 25.2230 25.2800 22.7500 attach(Mappe1) month <- Mappe1$month value

我想用x轴上的月份来绘制这个数据框

      month  value1  value2  value3  value4
    1   Okt 19.5505 19.6145 19.5925 19.3710
    2   Nov 21.8750 21.7815 21.7995 20.5445
    3   Dez 25.4335 25.2230 25.2800 22.7500

    attach(Mappe1)
   month <- Mappe1$month
   value1 <- Mappe1$value1
   value2 <- Mappe1$value2
   value3 <- Mappe1$value3
   value4 <- Mappe1$value4
使用ggplot2,我得到一条错误消息:错误:无效输入:time_trans仅适用于类POSIXct的对象

   ggplot(Mappe1, aes(x=month, y=value1)) + geom_point() +
     scale_x_datetime(breaks = date_breaks("1 month"), labels = date_format("%B")) + 
     xlab("month") + ylab("values") 

我是R工作室的新手,非常感谢您的帮助

除非你告诉R你的因子的水平是多少,否则它定义了它自己的顺序。当有一个固有的顺序时,它很擅长猜测,但这里的问题是,您的月份命名约定不适合基于英语拼写的R的约定。您可以将因子的名称改为英文,即在您给定的月份内的10月、11月和12月,或者,更一般地说,您可以自己定义因子

要执行后者,假设数据帧Mappe1:

tidyverse方法-

library(tidyverse)

data.raw = "month   value1  value2  value3  value4
Okt 19.5505 19.6145 19.5925 19.3710
Nov 21.8750 21.7815 21.7995 20.5445
Dez 25.4335 25.2230 25.2800 22.7500"

months <- tribble(
  ~month, ~result,
  "Okt", "Oct",
  "Nov", "Nov",
  "Dez", "Dec"
)

data = read_tsv(data.raw)

data %>%
  left_join(months) %>%
  mutate(month = as.Date(sprintf("2016-%s-01", result), "%Y-%b-%d")) %>%
  select(-result) %>%
  gather(series, value, -month) %>%
  ggplot(aes(month, value, colour = series)) +
  geom_line() +
  scale_x_date(date_breaks = "1 month", date_labels = "%B") + 
  xlab("month") + ylab("values") 
#> Joining, by = "month"

非常感谢您的回答! 我这样试过:

    t = read.csv("Mappe1.csv", header = TRUE, sep=";", dec = ".", fill = TRUE, comment.char = "")

    t$m <- factor(t$m, levels = c("Okt", "Nov", "Dez"))

    library(Hmisc)

    xyplot(t$value1~t$m, type = "l", col = "red", ylab="values")
    lines(t$value2~t$m, type = "l", col = "cyan")
    lines(t$value3~t$m, type = "l", col = "purple")
    lines(t$value4~t$m, type = "l", col = "black", lwd = 2)
    legend("topleft", legend=c("value1", "value2", "value3", "value4"),
       col=c("red", "cyan", "purple", "black"), lty=1:1, cex=0.8)
我已经应用了plot.new和dev.off。但有时我仍然会得到这些错误,或者有时R不显示错误,但根本不绘图


这里可能有什么问题

请添加创建Mappe1所需的代码,好吗?然后有人可以给你一个完整的答案。请记住,scale_x_datetime仅适用于日期类型的变量。您的月份变量不是日期而是一个因子>dputMappe1 structurelistV1=structurec2L,4L,3L,1L,.Label=cDez,month,Nov,Okt,class=factor,V2=structurec4L,1L,2L,3L,.Label=c19.5505,21.875,25.4335,value1,class=factor,V3=structurec4L,1L,2L,3L,.Label=c19.6145,21.7815,25.223,value2,class=factor,V4=structurec4L,1L,2L,3L,.Label=c19.5925,21.7995,25.28,value3,class=factor,V5=structurec4L,1L,2L,3L,.Label=c19.371,20.5445,22.75,value4,class=factor,.Names=cV1,V2,V3,V4,V5,class=data.frame,row.Names=cNA,-4L非常感谢您的快速回答!对于df$month,您需要将df更改为数据帧Mappe1的名称。在您的示例中,我认为从您最初的postNow可以看出,在相应月份的值位置有黑色水平条。它们与每个月的栏一样宽。我能把它们去掉吗?图中未显示值1。。。这可能是什么原因?这听起来像是绘图问题,而不是数据问题。为了绘制数据,您必须相应地进行设置。上面给出的命令仅为每个月绘制值1,而不是按月绘制所有值@上面迈克尔的回答为你提供了一个很好的例子,说明了你是如何策划这件事的。谢谢你的回答。我只是尝试了一下,但得到的错误是没有公共变量。请按参数指定。我该如何解决这个问题?也许值得在一个新问题中提出这个问题,我对xyplot没有太多经验。如果你想用ggplot来做这件事,你必须把你的数据分解成长格式,然后你就可以按时间把它画出来。
levels(Mappe1$month) <- c("Okt", "Nov", "Dez")
library(tidyverse)

data.raw = "month   value1  value2  value3  value4
Okt 19.5505 19.6145 19.5925 19.3710
Nov 21.8750 21.7815 21.7995 20.5445
Dez 25.4335 25.2230 25.2800 22.7500"

months <- tribble(
  ~month, ~result,
  "Okt", "Oct",
  "Nov", "Nov",
  "Dez", "Dec"
)

data = read_tsv(data.raw)

data %>%
  left_join(months) %>%
  mutate(month = as.Date(sprintf("2016-%s-01", result), "%Y-%b-%d")) %>%
  select(-result) %>%
  gather(series, value, -month) %>%
  ggplot(aes(month, value, colour = series)) +
  geom_line() +
  scale_x_date(date_breaks = "1 month", date_labels = "%B") + 
  xlab("month") + ylab("values") 
#> Joining, by = "month"
    t = read.csv("Mappe1.csv", header = TRUE, sep=";", dec = ".", fill = TRUE, comment.char = "")

    t$m <- factor(t$m, levels = c("Okt", "Nov", "Dez"))

    library(Hmisc)

    xyplot(t$value1~t$m, type = "l", col = "red", ylab="values")
    lines(t$value2~t$m, type = "l", col = "cyan")
    lines(t$value3~t$m, type = "l", col = "purple")
    lines(t$value4~t$m, type = "l", col = "black", lwd = 2)
    legend("topleft", legend=c("value1", "value2", "value3", "value4"),
       col=c("red", "cyan", "purple", "black"), lty=1:1, cex=0.8)
    Error in plot.xy(xy.coords(x, y), type = type, ...) : 
      plot.new has not been called yet
    Error in strwidth(legend, units = "user", cex = cex, font = text.font) : 
      plot.new has not been called yet