Reputation:
I'm unable to do it by myself, trying it for at least 2 hours...
I have the following data.frame imported from a CSV-file:
> asyl_continents
2010 2011 2012 2013 2014 2015 2016
Europe 1411 1352 2047 3277 5105 7647 5296
Asia 2832 3229 3903 4418 7123 32152 18002
America 20 17 20 27 35 42 97
Africa 436 757 897 1497 3846 5412 4592
Australia 0 0 0 0 0 0 1
stateless 34 34 35 28 80 88 179
unknown 159 160 140 207 360 408 505
This is the doutput of it:
structure(list(`2010` = c(1411L, 2832L, 20L, 436L, 0L, 34L, 159L
), `2011` = c(1352L, 3229L, 17L, 757L, 0L, 34L, 160L), `2012` = c(2047L,
3903L, 20L, 897L, 0L, 35L, 140L), `2013` = c(3277L, 4418L, 27L,
1497L, 0L, 28L, 207L), `2014` = c(5105L, 7123L, 35L, 3846L, 0L,
80L, 360L), `2015` = c(7647L, 32152L, 42L, 5412L, 0L, 88L, 408L
), `2016` = c(5296L, 18002L, 97L, 4592L, 1L, 179L, 505L)), class = "data.frame", row.names = c("Europe",
"Asia", "America", "Africa", "Australia", "stateless", "unknown"
))
I want to plot something like this: https://i.sstatic.net/l7s6l.png On the x-achsis there should be the years and for every category (Europe, Asia, ..., unknown) a seperate row.
Wondering what this is about? For a student-project I want to show that crimerates only gone up in absolute numbers with more refugees arriving, but not in relative numbers. Want to conbine this dataset later on with crimerates.
Upvotes: 0
Views: 372
Reputation: 594
The following code does not depend on non-base-R packages.
A) graphics::matplot
plots the columns of a matrix, to which the data-frame is coerced toautomatically:
matplot(x = as.numeric(colnames(asyl_continents)), # x values: the years
y = t(asyl_continents), # y values: one column per plotted line each, therefore transpose years' asylum values to columns
# col= c(...) # could specify your own colors here, as well as line types, thickness etc.
type = "l", # specify type: draw lines, see ? matplot for details
xlab = "years", ylab = "asylum")
B) legend()
adds a legend
legend(x="topleft", legend = row.names(asyl_continents),
col = seq_len(ncol(asyl_continents)), # use matplot's default color sequencs, unless spefified otherwise in matplot() (see https://stackoverflow.com/questions/27796583/how-to-add-colour-matched-legend-to-a-r-matplot)
lwd = 1, cex=0.85)
You can change colors, line types etc. as you want, see ? matplot for details.
Upvotes: 1
Reputation: 12739
This should set you off in the tidyverse direction to achieve the line graph you aspire to with your data.
library(tibble)
library(dplyr)
library(tidyr)
library(stringr)
library(ggplot2)
# Provide a sample of data in a proper dataframe or equivalent object
df <- tibble(cont = c("Europe", "Asia", "America", "Africa", "Australia", "stateless", "unknown"),
`2010` = c(1411L, 2832L, 20L, 436L, 0L, 34L, 159L),
`2011` = c(1352L, 3229L, 17L, 757L, 0L, 34L, 160L),
`2012` = c(2047L, 3903L, 20L, 897L, 0L, 35L, 140L),
`2013` = c(3277L, 4418L, 27L, 1497L, 0L, 28L, 207L),
`2014` = c(5105L,7123L, 35L, 3846L, 0L, 80L, 360L),
`2015` = c(7647L, 32152L, 42L, 5412L, 0L, 88L, 408L),
`2016` = c(5296L, 18002L, 97L, 4592L,1L, 179L, 505L))
# in this case it is helpful to put the data in to longer format
df1 <-
df %>%
pivot_longer(cols = matches("\\d{4}$"), names_to = "year", values_to = "asyl_nr")
# plot the data
ggplot(df1, aes(year, asyl_nr, colour = cont, group = cont))+
geom_line()+
theme_classic()
And this is what you end up with:
Upvotes: 0
Reputation: 1106
Welcome to stackoverflow. Please describe a little more what you want as a barplot ? Also, please give us the output of dput(asyl_continents)
so we can work with your data frame directly. The following code is 'by memory', i did not test it but i think i'll work.
Something like boxplot(asyl_continents)
?
by relative number, you mean rowwise increasing ? indeed, this is meaningfull :
df <- asyl_continents
df <- apply(df,1,function(x){1+ (x-x[1])/x[1]})
matplot(t(df))
Upvotes: 0