0

I have a dataframe of tempratures where each column represents a year from 1996 to 2015 and rows are data from 1-Jul to Oct-31:

head(df)

[![Dataframe head][1]][1]

I am trying to create a line plot with x= DAYS and y=temp per year. when I use DAYS in the loop, either with aes() or aes_strint() it doesn't produce anything:

iterator <- c(colnames(df))[-1]
g <- ggplot(df, aes_string(x = 'DAY'))

for (i in iterator){
  g <- g+ geom_line(aes_string(y=i))
          
}
print(g)

so I added an index column which is just integers from 1 to 123. Now the same code plots a bunch of lines but very strange:

df$index <- c(1:123)
iterator <- c(colnames(df))[-1]
iterator <- iterator[-21]
g <- ggplot(df, aes_string(x = 'index'))

for (i in iterator){
  g <- g+ geom_line(aes_string(y=i))
          
}
print(g)

[![Final plot][2]][2]

as you can see, I have one line per column name and all the Colum names are stacking above each other. This has compressed the vertical axis so much that the variations in temperature is not visible. I wish my y-axis just goes from 50 to 100 and there will be one line per column name there with the same scale as other columns. How do I do that? [1]: https://i.sstatic.net/ruF11.png [2]: https://i.sstatic.net/gAvMe.png

3 Answers 3

3

Agree with Andrew's solution. Just a minor change: you have to remove the "df" on 3rd line as you declared it already in the beginning.

df  %>%
  tidyr::pivot_longer(!DAYS, names_to = "column", values_to = "temp") %>% 
  ggplot(aes(x = DAYS, y = temp, group = column)) + 
  geom_line()
Sign up to request clarification or add additional context in comments.

Comments

1

I think you could rearrange your data frame, e.g. using the tidyr package, so that you have a data frame with "year", "day" and "temp" columns

library(ggplot2)
library(tidyr)

year1 = c(5,6,4,5)
year2 = c(6,5,5,6)
year3 = c(3,4,3,4)
date = c("a", "b", "c", "d")

data = data.frame(date, year1, year2, year3)

data2 = gather(data , "year", "temp", -date)

Then, you can easily plot the temperature per year.


ggplot(data2, aes(x = date, y = temp, group = year, color = year))+ 
  geom_path()

Temp per year

Comments

1

If you're doing something with loops in R, especially with ggplot2, you are probably doing something wrong. I'm not 100% sure why you're looping at all, when you probably want to do something more like,

df  %>%
  tidyr::pivot_longer(!DAYS, names_to = "column", values_to = "temp") %>% 
  ggplot(df, aes(x = day, y = temp, group = column)) + 
  geom_line() 

but without a reprex / data set I can't be sure if that's what you want.

2 Comments

Thanks Andrew. very helpful answer. A couple of quick none spelling corrections to the code though: you don't need df with pipes and ggplot. Also, DAY is a character column, so I needed to add a numerical index column to keep the order in the plot before unpivoting the data. here is the variation that worked for me: 'df$index <- c(1:123)df[2:22] %>% tidyr::pivot_longer(!index, names_to = "column", values_to = "temp") %>% ggplot( aes(x = index, y = temp, group = column)) + geom_line()`
oof yes sorry, I was typing this directly into SO without trying it and didn't see those redundancies. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.