0

I am struggling to plot a specific row from a dataframe. Below is the Graph i am trying to plot. I have tried using ggplot and normal plot but i cannot figure it out.

This is the output I'm am trying to plot

 Wt2 Wt3 Wt4 Wt5 Lngth2 Lngth3 Lngth4 Lngth5
1  48  59  95  82    141    157    168    183
2  59  68 102 102    140    168    174    170
3  61  77  93 107    145    162    172    177
4  54  43 104 104    146    159    176    171
5 100 145 185 247    150    158    168    175
6  68  82  95 118    142    140    178    189
7  68  95 109 111    139    171    176    175

Above is the Data frame I am trying to plot with. The rows are for each bears measurement. So row 1 is for bear 1. How would I plot only the Wt columns for bear 1 against an X-axis that goes from years 2 to 5

1
  • Another thing, it is more useful to post the data in a way we can run code with, so the next time post the output of dput(your_data_frame) instead of the console. Commented Mar 20, 2021 at 16:48

2 Answers 2

2

You can pivot your data frame into a longer format:

First add a column with the row number (bear number):

df = cbind("Bear"=as.factor(1:nrow(df)), df)

It needs to be factor so we can pass it as a group variable to ggplot. Now pivot:

df2 = tidyr::pivot_longer(df[,1:5], cols=2:5,
                          names_to="Year", values_to="Weight", names_prefix="Wt")
df2$Year = as.numeric(df2$Year)

We ignore the Length columns with df[,1:5]; say that we only want to pivot the weight columns with df[,2:5]; then say the name of the columns we want to create with names_to and values_to; and lastly the names_prefix="Wt" removes the "Wt" before the column names, leaving only the year number, but we get a character, so we need to make it numeric with as.numeric().

Then plot:

ggplot(df2, aes(x=Year, y=Weight, linetype=Bear)) + geom_line()

Output (Ps: i created my own data, so the actual numbers are off):

enter image description here

Just an addition, if you don't want to specify the columns of your dataset explicity, you can do:

df2 = df2[,grep("Wt|Bear", colnames(df)]
df2 = tidyr::pivot_longer(df2, cols=grep("Wt", colnames(df2)),
                          names_to="Year", values_to="Weight", names_prefix="Wt")

Edit: one plot for each group

You can use facet_wrap:

ggplot(df2, aes(x=Year, y=Weight, linetype=Bear)) +
  facet_wrap(~Bear, nrow=2, ncol=4) +
  geom_line()

Output:

enter image description here

You can change the nrow and ncol as you wish, and can remove the linetype from aes() as you already have a differenciation, but it's not mandatory.

You can also change the levels of the categorical data to make the labels on each graph better, do levels(df2$Bear) = paste("Bear", 1:7) for example (or do that the when creating it).

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much! Just one more question. Where in ggplot do we specify the factor variable? Is the the linetype part?
Yes! You can also use different aesthetics to differentiate the bears, such as color=Bear, alpha=Bear (transparency), etc... If it solved your problem, remember to accept the answer :)
Sorry one last question, how would one then create 7 different plots for each factor variable?
Just a clarification on my comment above for each level of the factor variable.
0

Try

ggplot(mapping = aes(x = seq.int(2, 5), y = c(48, 59, 95, 82))) +
  geom_point(color = "blue") +
  geom_line(color = "blue") +
  xlab("Year") +
  ylab("Weight")

1 Comment

In general, if you need to plot a specific row of a data frame, it would be helpful to have an id column. Most R plotting libraries will have functions with some sort of "by = " argument for group plotting.

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.