I am not sure if I have to change the format of my data to make a boxplot. My data looks like this:
Land Income 1999 Income 2019
Spain 5 7
France 4 8
Greece 3 5
...
Now I want to make two box plots to compare the income in 1999 and 2019. Is that possible? I don't know how I should choose the axes.
Thank you in advance
CodePudding user response:
There’s no need to reformat the data, you can create boxplots directly. Assuming your data is in a table called df
:
boxplot(df[, -1L])
CodePudding user response:
An approach with ggplot2:
library(tidyverse)
df <- data.frame(
country = c("Spain", "france", "Portugal"),
"income_1999" = c(9,8,7),
"income_2019" = c(11,10,9)
)
df %>%
pivot_longer(-country, names_to = "income", values_to = "amount") %>%
ggplot(aes(x=income, y=amount))
geom_boxplot()
Created on 2021-10-19 by the reprex package (v2.0.0)