简体   繁体   中英

Problem creating multiple line plot on ggplot2

I'm trying to create a plot showing the temporal evolution (x) of different values from the same column (y), thus requiring to create a plot with different lines. I am able to create separate plots for each value of y, so my problem seems to be specifically about adding multiple lines showing different values (of different lengths it seems).

This is the dput of the columns "Date" and "Journal" I use from my dataset "test" :

> structure(list(Date = structure(c(9132, 9136, 9136, 9141, 9141, 
9142), class = "Date", tzone = "Europe/Paris"), Journal = c("Libération", 
"Libération", "Libération", "Libération", "Le Monde", "La Tribune (France)"
)), row.names = c(NA, -6L), .internal.selfref = <pointer: 0x000002146c471ef0>, class = c("data.table", 
"data.frame")) 

I used the following code to successfully create a barplot which shows the evolution of column "Journal" according to the column "Date".

dateplot <- ggplot(cleantest) + aes(x = format(Date, "%Y%")) + geom_bar()

I also managed to create single line plots for each value of Y, with the following code :

valueplot <- ggplot(subset(test, Journal %in% "value")) + aes(x = format(Date, "%Y")) + geom_line(stat = "count", na.rm = TRUE, group = 1)

Therefore, I typed the following codes to obtain, for example, two lines in the same plot, and each of them returned a different error :

jourplot <- ggplot(test, aes(x = format(Date, "%Y"))) + geom_line(aes(y = subset(test, Journal %in% "Libération"), colour = "blue")) + geom_line(aes(y = subset(test, Journal %in% "Le Figaro"), colour =
   "red"))

The error is :

 > Don't know how to automatically pick scale for object of type data.table/data.frame. Defaulting to continuous. Erreur : Aesthetics must be either length 1 or the same as the data (17307): y

So I tried this :

jourplot <- ggplot(test, aes(x = format(Date, "%Y")) + geom_line(aes(y = subset(test, Journal %in% "Libération"), colour = "blue"), stat = "count", na.rm = TRUE, group = 1) + geom_line(aes(y = subset(test, Journal %in% "Le Figaro"), colour = "red"), stat = "count", na.rm = TRUE, group = 1"))

But this one doesn't even create the "jourplot" object. There is obviously something wrong with my code and / or my data, but as a newbie I really don't see it. It seems to be about length, but how do I get over this ? Or is this about the classes of the columns that make things difficult to process for ggplot ?

Does anyone understand what is going on ?

Edit : I deleted the "+" symbols from prompt

Is it your full dataset ? To my opinion your example seems to be too small to get a sense of what you are trying to plot.

From my understanding, you are trying to plot the count of each journal per year. But your example is covering only few points for 1995 with some journal with an unique value, so I don't think you can get a line with one point.

Here, I simulate a dataframe with a sequence of dates covering every week for five years and I attributes randomly for each week, one of three journals. Then, I formated the date sequence per year and I plot the count for each year as follow:

library(lubridate)
rep_df <- data.frame(Date = seq(ymd("1995-01-01"),ymd("2000-01-01"), by = "weeks"),
                     Journal = sample(c("Liberation","Le Monde","Le Figaro"), 261, replace = TRUE))
rep_df$Year <- floor_date(rep_df$Date, unit = "year")
head(rep_df)

        Date    Journal       Year
1 1995-01-01   Le Monde 1995-01-01
2 1995-01-08  Le Figaro 1995-01-01
3 1995-01-15 Liberation 1995-01-01
4 1995-01-22   Le Monde 1995-01-01
5 1995-01-29 Liberation 1995-01-01
6 1995-02-05 Liberation 1995-01-01
library(ggplot2)
ggplot(rep_df, aes(x = Year))+
  geom_point(aes(color = Journal), stat = "count")+
  geom_line(aes(color = Journal),stat = "count")+
  scale_x_date(date_labels = "%Y", date_breaks = "1 year", name = "")

在此处输入图片说明

Does it look what you are trying to get ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM