-1

I want to plot this data frame with month on x-axis.

      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

I tried different solutions, but with this code the months are plottet reversed (Dez, Nov, Okt, insted of Okt, Nov, Dez):

   plot(x=month, y=value1, type="l", col="red")
   lines(x=month, y=value2, type="l", col="seagreen")
   lines(x=month, y=value3, type="l", col="cyan")
   lines(x=month, y=value4, type="l", col="black", lwd=2)

using ggplot2 I get an error message: Error: Invalid input: time_trans works with objects of class POSIXct only

   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") 

I am very new to R Studio and very thankful about any help!

Axeman
  • 32,068
  • 8
  • 81
  • 94
N_ni
  • 27
  • 4
  • 1
    Could you please add the code needed to create `Mappe1`? Then someone can give you a complete answer. Do this with `dput(Mappe1)`. – Bobby Nov 01 '16 at 15:06
  • 2
    Possible duplicate of [Change the order of a discrete x scale](http://stackoverflow.com/questions/3253641/change-the-order-of-a-discrete-x-scale) – Bobby Nov 01 '16 at 15:06
  • Keep in mind, `scale_x_datetime` only works on variables of type `Date`. Your `month` variable is not a `Date` but a `factor` – yeedle Nov 01 '16 at 15:09
  • > dput(Mappe1) structure(list(V1 = structure(c(2L, 4L, 3L, 1L), .Label = c("Dez", "month", "Nov", "Okt"), class = "factor"), V2 = structure(c(4L, 1L, 2L, 3L), .Label = c("19.5505", "21.875", "25.4335", "value1" ), class = "factor"), V3 = structure(c(4L, 1L, 2L, 3L), .Label = c("19.6145", "21.7815", "25.223", "value2"), class = "factor"), V4 = structure(c(4L, 1L, 2L, 3L), .Label = c("19.5925", "21.7995", "25.28", – N_ni Nov 01 '16 at 19:37
  • "value3" ), class = "factor"), V5 = structure(c(4L, 1L, 2L, 3L), .Label = c("19.371", "20.5445", "22.75", "value4"), class = "factor")), .Names = c("V1", "V2", "V3", "V4", "V5"), class = "data.frame", row.names = c(NA, -4L)) – N_ni Nov 01 '16 at 19:38

3 Answers3

1

Unless you tell R what the levels of your factor is, it defines its own order. It's pretty good at guessing when there is an inherent order, but the problem here is stemming from the fact that your month naming convention does not suit R's convention, which is based on English spellings. You can either change the naming of your factors into English, which would be "Oct", "Nov" and "Dec" for the months you've given, or else, more generally, define the factors yourself.

To do the latter, assuming data frame Mappe1:

Mappe1$month <- factor(Mappe1$month, levels = c("Okt", "Nov", "Dez"))

Add to the c() all of the months you have, in the appropriate order.

You could also use the function levels(), like so

levels(Mappe1$month) <- c("Okt", "Nov", "Dez")
Henry Brice
  • 280
  • 1
  • 2
  • 18
  • Thank you very much for your fast answer! With df$month <- factor(df$month, levels = c("Okt", "Nov", "Dez")) I got the following error: object of type 'closure' is not subsettable. But without "df" it works very well! – N_ni Nov 01 '16 at 15:26
  • You need to change `df` to the name of your data frame (`Mappe1` in your case, I think from your original post) – Henry Brice Nov 01 '16 at 15:31
  • Now there are black horizontal bars at the position of the values of the corresponding month. They are as broad as the column of each month. Can I remove them somehow? And value1 isn't shown in the plot... What could be the reason for this? – N_ni Nov 01 '16 at 16:09
  • That sounds like an issue with your plotting, rather than the data. In order to plot your data, you'll have to set it up accordingly. The command you give above is only plotting value1 for each month, not all values by month. @Michael's answer above gives a good example of how you could plot this. – Henry Brice Nov 01 '16 at 18:29
1

The tidyverse approach -

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"

tidyverse

Michael Griffiths
  • 1,399
  • 7
  • 14
  • Thank you for your answer. I just tried it but got the error " No common variables. Please specify `by` param." How can I solve this? – N_ni Nov 01 '16 at 15:49
0

Thank you very much for your answers! I tried it this way:

    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)

it worked out very well for this example. but when I tried it exactely the same way but with different values, only value1 is plottet and I always get the following errors:

    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

I already applied plot.new() and dev.off(). But sometimes I still get these errors or sometimes R doesn't show errors but doesn't plot at all.

What might be the problem here?

N_ni
  • 27
  • 4
  • Might be worth asking this in a new question, I do not have much experience with xyplot. If you want to do this with ggplot, you'd have to `melt()` your data down to long format, and then you could plot it out by time. – Henry Brice Nov 03 '16 at 17:37