本文将讨论用R语言自带的基本绘图系统和ggplot2包来讨论基本图形的绘制。
一、条形图(bar chart):
我在学习这部分时会遇到这样的困惑,觉得条形图(bar chart)和直方图(histogram)差不多,两者都是用来统计频数的,histogram还可以用来统计频率。简单来说,条形图注重分类,而直方图更多用于绘制一系列有意义的连续型变量,具体例子见下文。Both charts display a summary value of a continuous variable that has been split into groups.
In bar charts, the groups are typically categorical variables.
In histograms the groups are typically intervals of another continuous variable.
# of people enrolled in each department of a college would suit a bar chart
# of people in each income quintile in your city would suit a histogram
One implication of this difference is that a natural order exists on the grouping axis of a histogram, but not a bar chart. In other words, it usually makes sense to sort a bar chart by value of the bar but a histogram should almost always remain sorted by the order of the groups. Using the examples above:
It would make sense to order the college departments from highest to lowest enrollment.
It wouldn't make as much sense to order the income quintiles by most to least people, you would end up with a counter-intuitive graph.
Bar charts usually have a space between the bars, histograms usually don't - reflecting the subtle differences in the relationships between adjacent groups.
—— Excerps from QUORA
见代码,这里使用vcd包里的Arthritis数据框作例子,先来看一下它里面有些啥:
> library(vcd)
载入需要的程辑包:grid
> head(Arthritis)
ID Treatment Sex Age Improved
1 57 Treated Male 27 Some
2 46 Treated Male 29 None
3 77 Treated Male 30 None
4 17