Reputation: 1
This is my data. (click the link) I want to plot a bar chart in R which the x-axis is the income and the y-axis is the Degree (undergraduate, master...).Also, each bar in one group is represented by region. Could you tell me how to get the bar plot in R? Thank you so much for answering.
Upvotes: 0
Views: 61
Reputation: 4993
So, that is not really a typical use of a barchart, the Y column should be numerical a frequency (count), a sum of some kind or an aggregate (percent) and the X should be categories (unless you make a horizontal bar chart. Adding that third vector is not a problem at all you can do it by adding to your aes()
another dimension and you will get a 'stacked' chart where each region
has a sub-bar within the particular bar for each degree Degree
aes(x = Degree, y=income, fill = region))
but my guess is you will need to do some manipulation first such as
new_df<-df%>%
group_by(region, degree)%>%
summarize(total_income=sum(income)
This will get you a total by region for each degree so that the chart knows what to do with the data. You can also us mean or median instead of sum, whatever works for your needs
Upvotes: 0
Reputation: 123
Using @br00t's data table: you could also do facets rather than a side-by-side bar chart
plot <- ggplot(data = sample_df, mapping = aes(x=degree, y=income))+
geom_bar(stat = "identity")+
facet_wrap(~province)
plot
Upvotes: 1
Reputation: 1614
In future, please use dput
to share your data as it is quite onerous to take sample data from an image file.
library(ggplot2)
sample_df <- data.frame(
year = c(2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L),
province = c('Newfoundland and Labrador', 'Newfoundland and Labrador',
'Newfoundland and Labrador', 'Newfoundland and Labrador',
'Newfoundland and Labrador', 'Newfoundland and Labrador',
'Prince Edward Island', 'Prince Edward Island',
'Prince Edward Island ', 'Prince Edward Island ', 'Prince Edward Island',
'Prince Edward Island', 'Nova Scotia', 'Nova Scotia', 'Nova Scotia',
'Nova Scotia', 'Nova Scotia', 'Nova Scotia', 'New Brunswick',
'New Brunswick', 'New Brunswick', 'New Brunswick', 'New Brunswick',
'New Brunswick', 'Quebec', 'Quebec', 'Quebec', 'Quebec', 'Quebec',
'Ontario', 'Ontario', 'Ontario', 'Ontario', 'Ontario'),
code = c('2016A000210', '2016A000210', '2016A000210', '2016A000210', '2016A000210',
'2016A000210', '2016A000211', '2016A000211', '2016A000211', '2016A000211',
'2016A000211', '2016A000211', '2016A000212', '2016A000212', '2016A000212',
'2016A000212', '2016A000212', '2016A000212', '2016A000213', '2016A000213',
'2016A000213', '2016A000213', '2016A000213', '2016A000213', '2016A000224',
'2016A000224', '2016A000224', '2016A000224', '2016A000224', '2016A000235',
'2016A000235', '2016A000235', '2016A000235', '2016A000235'),
degree = c('Career, technical or professional training diploma',
'Career, technical or professional training diploma',
'Undergraduate degree', 'Professional degree', 'Master\'s degree',
'Doctoral degree', 'Career, technical or professional training certificate',
'Career, technical or professional training diploma', 'Undergraduate degree',
'Professional degree', 'Master\'s degree', 'Doctoral degree',
'Career, technical or professional training certificate', 'Career, technical or professional training diploma',
'Undergraduate degree', 'Professional degree', 'Master\'s degree',
'Doctoral degree', 'Career, technical or professional training certificate',
'Career, technical or professional training diploma', 'Undergraduate degree',
'Professional degree', 'Master\'s degree', 'Doctoral degree',
'Career, technical or professional training diploma', 'Undergraduate degree',
'Professional degree', 'Master\'s degree', 'Doctoral degree',
'Career, technical or professional training certificate',
'Career, technical or professional training diploma',
'Undergraduate degree', 'Professional degree', 'Master\'s degree'
),
income = c(30900L, 44300L, 56600L, 96500L, 79400L, 73900L, 37300L,
35100L, 39500L, 80400L, NA, 89500L, 30700L, 36000L, 44600L, 79900L,
67700L, 71200L, 28000L, 38300L, 47800L, 58800L, 72600L, 68800L,
38900L, 47500L, 69800L, 59900L, 55700L, 29900L, 36000L, 45600L,
83300L, 65700L)
)
p <- ggplot(data = sample_df, aes(x = degree, y = income, fill = province)) +
geom_bar(stat = 'identity', position = position_dodge())
p + coord_flip()
Output:
Upvotes: 3