Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

data visualization - How to combine different barplot in R?

I am a new R user.

I have a difficult time figuring out how to combine different barplot into one graph.

For example,

Suppose, the top five of professions in China, are, government employees, CEOs, Doctors, Athletes, artists, with the incomes (in dollars) respectively, 20,000,17,000,15,000,14,000,and 13,000, while the top five of professions in the US, are, doctors, athletes, artists, lawyers, teachers with the incomes (in dollars) respectively, 40,000,35,000,30,000,25,000 and 20,000.

I want to show the differences in one graph.

How am I supposed to do that? Beware that they have different names.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The answer to the question is fairly straight forward. As a new R user, I recommend you make liberal use of the 'ggplot2' package. For many R users, this one package is enough.

To get the "combined" barchart described in the original post, the answer is to put all of the data into one dataset and then add grouping variables, like so:

Step 1: Make the dataset.

data <- read.table(text="
Country,Profession,Income
China,Government employee,20000
China,CEO,17000
China,Doctor,15000
China,Athlete,14000
China,Artist,13000
USA,Doctor,40000
USA,Athlete,35000
USA,Artist,30000
USA,Lawyer,25000
USA,Teacher,20000", header=TRUE, sep=",")

You'll notice I'm using the 'read.table' function here. This is not required and is purely for readability in this example. The important part is that we have our values (Income) and our grouping variables (Country, Profession).

Step 2: Create a barchart with Income as the height of the bars, Profession as the x-axis, and color the bars by Country.

library(ggplot2)

ggplot(data, aes(x=Profession, y=Income, fill=Country)) +
    geom_bar(stat="identity", position="dodge") +
    theme(axis.text.x = element_text(angle = 90))

enter image description here

Here we are first loading the 'ggplot2' package. You may need to install this.

Then, we specify what data we want to use and how to separate it.

ggplot(data, aes(x=Profession, y=Income, fill=Country))

This tells 'ggplot' to use our dataset in the 'data' data frame. The aes() command specifies how 'ggplot' should read the data. We map the grouping variable Profession onto the x-axis, map the Income onto the y-axis, and change the color (fill) of each bar according to the grouping variable Country.

Next, we specify what kind of barchart we want.

geom_bar(stat="identity", position="dodge")

This tells 'ggplot' to make a barchart (geom_bar()). By default, the 'geom_bar' function tries to make a histogram, but we already have the totals we want to use. We tell it to use our totals by specifying that the type of statistic represented in Income is the total, or actual values (identity) that we want to chart (stat="identity"). Finally, I made a judgement call about how to display the data and decided to set one set of data on next to the other when a single profession has multiple income values (position="dodge").

Finally, we need to rotate the x-axis labels, since some of them are quite long. We do this with a simple 'theme' command that changes the rotation of the x-axis text elements.

theme(axis.text.x = element_text(angle = 90))

We chain all of these commands together with the +, and it's done!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...