01 六月, 2020

Outline

  • Introduction

  • Graphs for display of single variable

  • Graphs comparing two variables

  • Graphs illustrating more than two variables

Introduction

  • describe appropriate use of graphs.

  • recommend particular types of graph.

  • describe why they are good choices

  • All the examples are drawn from NHANES 2003–2004 and 2005–2006 datasets and 「金錢誘因、動機與教師研究生產力」

-「金錢誘因、動機與教師研究生產力」來源為「學術調查研究資料庫」(Survey Research Data Archive),由國立臺灣大學教務處師資培育中心為探討金錢誘因與教師研究表現之間的關係,以我國9所大學493位專任教師為對象,蒐集人口變項、動機和長期研究生產力資料。

Stacked dot chart (n=30)

library("beeswarm")
with(nhanessmall, beeswarm(BPXSAR, breaks=NA, vertical=FALSE, pch=1, method="center", xlab="Systolic BP (mmHg)"))

Stacked dot chart(n=200)

library("beeswarm")
with(nhanesmedium, beeswarm(BPXSAR, breaks=NA, vertical=FALSE, pch=1, method="center", xlab="Systolic BP (mmHg)"))

Violin plot(n=1000)

nhaneslarge <- read.csv("http://faculty.washington.edu/kenrice/heartgraphs/nhaneslarge.csv", na=".")
library("sm")
library("vioplot")
vioplot(nhaneslarge$BPXSAR, h=3, names="", horizontal=TRUE, col="grey90", drawRect=FALSE)
mtext(side=1, line=3, "Systolic BP (mmHg)")
points(x=mean(nhaneslarge$BPXSAR), y=1, pch=16, col="blue")
points(x=median(nhaneslarge$BPXSAR), y=1, pch=18, col="red")
legend("topright", pch=c(16,18), col=c("blue","red"), c("Sample mean","Sample median"), bty="n")

大學專任教師每周平均研究時數

ggplot(dta1, aes(a5_2_1,b7)) +geom_boxplot(col='gray') +geom_point() +labs(x='每周平均研究時數', y='是否贊成彈性薪資')+theme_bw()

Graphs for display of categorical variables

Dot chart of proportion

dotchart( 1-mean( nhaneslarge$BPXSAR>140), xlim=c(0.03,0.97), lcolor=NA,  
xlab="Proportion with Systolic BP <= 140 mmHg,\nwith corresponding 95% conf interval", pch=1)
lines(x = 1-binom.test( sum(nhaneslarge$BPXSAR>140), 1000)$conf.int, y=c(1,1))

Stacked bar chart

par(mar=c(4,2,4,0.3)+0.3, xpd=TRUE)
barplot(as.matrix(table(nhaneslarge$race_ethc)/1000), beside=FALSE, horiz=TRUE, xlim=c(0,1),
xlab="Proportion\n")
legend("top", inset=c(0, -0.5), fill=gray.colors(4), horiz=TRUE, bty="n", levels(nhaneslarge$race_ethc))

大學教師喜歡的獎勵方式(proportion)

dotchart(prop.table(table(dta3)))

Graphs comparing two variables

Continuous versus categorical

Multiple binned stacked dot chart

boxplot(DR1TFOLA ~gender, ylim=c(0,2200), type="n", border=0,
     data=nhanesmedium,xlab="",ylab=quote("Folate intake"~(mu*g/day)) )
beeswarm(DR1TFOLA ~gender, data=nhanesmedium,method="center", add=TRUE)

大學教師對金錢獎勵的態度與每周教學時間關係

ggplot(dta2, aes(b10_3, a5_1_1)) +geom_linerange(aes(x = b10_3, ymin = 0, ymax =a5_1_1), color = "lightgray", size = 1.5)+geom_point(aes(color = b10_3,position="jitter"), size = 2)+ggpubr::color_palette("jco")+labs(x="對金錢獎勵的態度", y="每周研究時數")+theme(legend.position = "none")+scale_colour_discrete("對金錢獎勵的態度")+theme_pubclean()