Create line graph with multiple lines in R

0 votes
I'd like to visualise census data in order to compare data for each race throughout time.

Years 1950-2010 (every 10 years) are the rows in my data frame, and race is the column. The percentage of that race in a given year is shown by the data at the cross section.

I want the years on the x axis and the race on the y axis in my line graph. So, with my five "race" factors, I'd have five different coloured lines shown on the same graph.

I've tried watching videos and looking around, but nothing I've found appears to operate the way I want it to.

Edit: Instead of using a dataframe, I refactored the code and created my own.
Jun 17, 2022 in Data Analytics by Avinash
• 1,260 points
300 views

1 answer to this question.

0 votes
You must first restructure your dataset into a longer format using tools like tidyr's pivot longer function. Your data ought to seem like this in the end.

The first line will convert all of your data into a numerical format that is much more suitable for plotting as your data are in factor format (with the exception of the Year column).

library(dplyr)\slibrary(tidyr)

Reshaped DF - df percent > percent pivot longer(-Year, names to = "Races", values to = "values")% > percent mutate at(vars(ends with(".pct")), as.numeric(as.character(.))%)%

# A tibble: values for 35 x 3 Year Races

1950 White.pct = 98.3; 1950 BlackOrAA.pct = 1.3; 1950 Hispanic.pct = 0; 1950 AsianOrPI.pct = 0; 1950 Other.pct = 1.3; 1960 White.pct = 98; 1950 Other.pct = 1.3 1960 Hispanic.pct 0 9 1960 BlackOrAA.pct 1.2 8 with 25 additional rows 1960 AsianOrPI.pct 0 10 1960 Other.pct 1.2 #
You can then use ggplot2
answered Jun 24, 2022 by Sohail
• 3,040 points

Related Questions In Data Analytics

0 votes
1 answer

How to print new lines with print() in R?

You can use cat() instead of writeLines(): ...READ MORE

answered May 3, 2018 in Data Analytics by kappa3010
• 2,090 points
578 views
0 votes
1 answer

How can I Split code over multiple lines in an R script?

You can do this as follows: setwd(paste("~/a/very/long/path/here ...READ MORE

answered May 22, 2018 in Data Analytics by zombie
• 3,790 points
1,729 views
+5 votes
0 answers
0 votes
1 answer

create data.frame with random numbers in R

First create a matrix with random numbers ...READ MORE

answered Sep 24, 2019 in Data Analytics by Debasmita Das
2,707 views
0 votes
1 answer

How to plot side-by-side Plots with ggplot2 in R?

By Using gridExtra library we can easily ...READ MORE

answered Apr 16, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 9, 2020 by MD 8,496 views
+1 vote
1 answer

How to change fill color in each facet using ggplot2?

You can map the facetting variable to ...READ MORE

answered May 8, 2018 in Data Analytics by kappa3010
• 2,090 points
24,620 views
0 votes
11 answers

Changing the legend title in ggplot

Hi, you can also try guides() to ...READ MORE

answered Jul 30, 2019 in Data Analytics by Cherukuri
• 33,030 points
16,896 views
0 votes
1 answer

How to order bars in a bar graph using ggplot2?

The key to ordering is to set ...READ MORE

answered Jun 1, 2018 in Data Analytics by DataKing99
• 8,240 points
909 views
0 votes
1 answer

How can I create a correlation matrix in R?

An example, d <- data.frame(x1=rnorm(10), ...READ MORE

answered Jun 14, 2022 in Data Analytics by Sohail
• 3,040 points
320 views
0 votes
1 answer

Use of $ and %% operators in R

According to help('percent in percent'), percent in ...READ MORE

answered Jun 23, 2022 in Data Analytics by Sohail
• 3,040 points
392 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP