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 283 views

## 1 answer to this question.

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
• 3,040 points

## 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

## "Error in tagAssert: argument "body" is missing, with no default" when trying to create a dashboard using shiny R

I'm trying to create a dashboard using ...READ MORE

## create data.frame with random numbers in R

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

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

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

+1 vote

## How to change fill color in each facet using ggplot2?

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

## Changing the legend title in ggplot

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

## How to order bars in a bar graph using ggplot2?

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