3.1 Basic Bar chart
Library (GGPLOT2)
Library (Gcookbook)
Pg_mean #这是用到的数据
Group weight
1 Ctrl 5.032
2 Trt1 4.661
3 Trt2 5.526
Ggplot (Pg_mean, AES (X=group, Y=weight)) + Geom_bar (stat= "Identity")
The x-axis is a continuous variable or a factor, and the graph is different, and the group here is the factor.
STR (Pg_mean)
' Data.frame ': 3 obs. of 2 variables:
$ group:factor W/3 Levels "Ctrl", "Trt1",..: 1 2 3 #可以看出group是因子
$ weight:num 5.03 4.66 5.53
Set the fill color with fill, set the border color with color
Ggplot (Pg_mean, AES (X=group, Y=weight)) + Geom_bar (stat= "Identity", fill= "LightBlue", color= "Black")
Use my pedometer data to try it out:
Sys.setenv (java_home= ' C:/Program files/java/jdk1.6.0_33/jre ')
Library (XLSX)
SETWD ("D:/shenlb/health")
Fitbit <-Read.xlsx (file= "fitbit2014.xlsx", Header=true, Sheetindex=1) #用到JAVA, a lot slower than read.csv
Meanmonthstep <-Aggregate (Fitbit$step, by=list (Format (fitbit$date, "%m")), mean)
Colnames (Meanmonthstep) <-C ("Month", "step") #设置列名
Ggplot (Meanmonthstep, AES (X=month, Y=step)) + Geom_bar (stat= "Identity", fill= "LightBlue", color= "Black")
3.2 Grouping Bars Together
Cabbage_exp
cultivar Date Weight sd n SE
1 c39 d16 3.18 0.9566144 10 0.30250803
2 c39 d20 2.80 0.2788867 10 0.08819171
3 c39 D21 2.74 0.9834181 10 0.31098410
4 c52 D16 2.26 0.4452215 10 0.14079141
5 c52 d20 3.11 0.7908505 10 0.25008887
6 c52 D21 1.47 0.2110819 10 0.06674995
The x-axis of a bar chart is usually a categorical variable, the y-axis is a continuous variable, and often provides another categorical variable for grouping comparisons, where cultivar is used to place the fill attribute (in fact, other display styles are available, but the fill color is the easiest to distinguish between different visualizations). Use the Dodge option to hide them from each other.
Ggplot (Cabbage_exp, AES (X=date, Y=weight, Fill=cultivar)) + Geom_bar (stat= "Identity", position= "Dodge")
If the position= "Dodge" option is not used, it is a stacked bar chart.
Ggplot (Cabbage_exp, AES (X=date, Y=weight, Fill=cultivar)) + Geom_bar (stat= "Identity")
You can also populate it with a different palette:
Ggplot (Cabbage_exp, AES (X=date, Y=weight, Fill=cultivar)) +
Geom_bar (stat= "Identity", position= "Dodge", color= "black") +
Scale_fill_brewer (palette= "Pastel1")
3.3. Making a Bar Graph of Counts
Head (Diamonds)
Carat Cut color clarity depth table price x y z
1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
5 0.31 good J SI2 63.3 58 335 4.34 4.35 2.75
6 0.24 Very good J VVS2 62.8 57 336 3.94 3.96 2.48
If you just want to count the number of occurrences by a categorical variable:
Ggplot (Diamonds, AES (X=cut)) + Geom_bar ()
It is actually equivalent to the following command:
Ggplot (Diamonds, AES (X=cut)) + Geom_bar (stat= "bin")
The x-axis of the above example uses categorical variables, and if you use continuous variables, you get a histogram.
Ggplot (Diamonds, AES (X=price)) + Geom_bar (stat= "bin")
At this time it is best to use Geom_histogram ():
Ggplot (Diamonds, AES (X=price)) + Geom_histogram ()
3.4. Using Colors in a Bar Graph
Fills the step data with the specified color. It's only 11 months, so it's made up of 11 colors.
Ggplot (Meanmonthstep, AES (X=month, Y=step, fill=month)) +
Geom_bar (stat= "Identity", color= "black") +
Scale_fill_manual (Values=c ("#111111", "#222222", "#333333", "#444444", "#555555", "#666666",
"#777777", "#888888", "#999999", "#AAAAAA", "#BBBBBB"))
If you want to remove the legend on the right, use the Guide=false
Ggplot (Meanmonthstep, AES (X=month, Y=step, fill=month)) +
Geom_bar (stat= "Identity", color= "black") +
Scale_fill_manual (Values=c ("#111111", "#222222", "#333333", "#444444", "#555555", "#666666",
"#777777", "#888888", "#999999", "#AAAAAA", "#BBBBBB"), Guide=false)
Add text label
Ggplot (Meanmonthstep, AES (X=month, Y=step)) +
Geom_bar (stat= "Identity", fill= "LightBlue", color= "black") +
Geom_text (Aes (Label=floor (STEP)), vjust=-0.2)
R Graphics Cookbook 3rd Chapter –bar Graphs