0

I have data that looks something like this (except with 90-ish rows).

test <- data.frame("Site_No" = c("01370", "01332", "01442"),"0.99" = c(12, 15, 18), "0.98" = c(14, 15, 18), "0.90" = c(7, 22, 30))

I would like to create 3 separate line plots using ggplot2. The x-axis would be 0.99, 0.98, 0.90 (aka the column names of my data frame). The y-axis would be the range of the values in the columns (so a range from 7 to 30). I would like a plot for each of my Site_No (which are station numbers: 01370, 01332, 01442).

I am trying my best to figure this out on my own and I'm having no luck because of the structure of my data frame.

Thank you for your help!

1 Answer 1

2

I usually use the data.table package and the 'melt' function to get a key-value format.
This format is much better for ggplot2:

# Your test data: (notice that i transformed the rownames into a column)
test <- data.frame("0.99" = c(12, 15, 18), "0.98" = c(14, 15, 18), "0.90" = c(7, 22, 30))
test$rownames <- c("01370", "01332", "01442")

# melt and plot:
dt <- data.table::as.data.table(test)

melted <- data.table::melt(dt, measure = c("X0.99","X0.98","X0.90"))

ggplot2::ggplot(data = melted, mapping = aes(x = variable, y = value, group = rownames)) + 
    ggplot2::geom_line() + 
    ggplot2::facet_grid(rows = vars(rownames))

EDIT: based on your edited question:

test <- data.frame("Site_No" = c("01370", "01332", "01442"),"0.99" = c(12, 15, 18), "0.98" = c(14, 15, 18), "0.90" = c(7, 22, 30))

dt <- as.data.table(test)

melted <- data.table::melt(dt, measure = c("X0.99","X0.98","X0.90"))

ggplot2::ggplot(data = melted, mapping = aes(x = variable, y = value, group = Site_No)) +
    ggplot2::geom_line() + 
    ggplot2::facet_grid(rows = vars(Site_No))

EDIT2: Based on your second comment: Create new plots for each group:

test <- data.frame("Site_No" = c("01370", "01332", "01442"),"0.99" = c(12, 15, 18), "0.98" = c(14, 15, 18), "0.90" = c(7, 22, 30))

dt <- as.data.table(test)

melted <- data.table::melt(dt, measure = c("X0.99","X0.98","X0.90"))

for (i in unique(melted$Site_No)){
    dev.new()
    print(ggplot2::ggplot(data = melted[Site_No == i,], mapping = aes(x = variable, y = value, group = Site_No)) +
        ggplot2::geom_line())
}
Sign up to request clarification or add additional context in comments.

7 Comments

Hi! I realized it's actually not the rownames of my data. I have a column called "Site_No" in my data. I updated the table above to correct the mistake. This code isn't working even when I change "rownames" to "Site_No".
It is working perfectly fine if you replace it. Have a look at my edit.
Yes it is working! Thank you. Is there anyway to have the plots not all be on one page? I'd like them to be separate because in reality, my data has 18 columns and 100 rows. So 100 plots on 1 page won't work. Thank you again!
Watch my second edit :) Kindly, accept the answer so it might help others too.
Sorry one more question lol. Is there a quick way to save all of these plots to my local drive?
|

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.