堆叠条形图,用于显示单打、双打、三打和本垒打的百分比
Stacked Bar Plot for Showing What Percent of Hits are Singles, Doubles, Triples, and Homers
我正在尝试制作一个堆积条形图,显示每支球队的单打、双打、三打和本垒打的百分比。但是,正如您从所附图片中看到的那样,我一直得到一个奇怪的结果。我想我在融化步骤上出错了,因为我不确定如何使用该功能。该图,如果做得正确,每行应该有四个堆叠条,每行所有四个条的总长度等于一个。请注意,您必须滚动才能查看更多代码。
如果能帮助解决此问题,我们将不胜感激。
df <- read.table(textConnection(
'Team Doubles Triples HR Singles
ARI 0.192697769 0.037863421 0.128465179 0.640973631
ATL 0.21011396 0.019230769 0.086894587 0.683760684
BAL 0.187544232 0.004246285 0.179051663 0.62915782
BOS 0.214643304 0.015644556 0.130162703 0.639549437
CHC 0.2079489 0.021291696 0.141234918 0.629524485
CHW 0.193977591 0.023109244 0.117647059 0.665266106
CIN 0.19743407 0.023521026 0.116892373 0.66215253
CLE 0.214634146 0.020209059 0.128919861 0.636236934
COL 0.205958549 0.030440415 0.132124352 0.631476684
DET 0.170731707 0.020325203 0.14295393 0.66598916
HOU 0.212874909 0.021214338 0.144842721 0.621068032
KCR 0.182068966 0.022758621 0.10137931 0.693793103
LAA 0.19787234 0.014184397 0.110638298 0.677304965
LAD 0.197674419 0.015261628 0.137354651 0.649709302
MIA 0.17739726 0.028767123 0.087671233 0.706164384
MIL 0.191685912 0.014626636 0.149345651 0.644341801
MIN 0.204400284 0.024840312 0.141944642 0.628814762
NYM 0.178837556 0.014157973 0.162444113 0.644560358
NYY 0.177793904 0.014513788 0.132801161 0.674891147
OAK 0.199704142 0.015532544 0.125 0.659763314
PHI 0.177011494 0.026819923 0.123371648 0.672796935
PIT 0.194249649 0.022440393 0.107293128 0.67601683
SDP 0.201568627 0.020392157 0.138823529 0.639215686
SEA 0.173582296 0.01175657 0.154218534 0.6604426
SFG 0.194850383 0.037578288 0.090466249 0.67710508
STL 0.21130742 0.022614841 0.159010601 0.607067138
TBR 0.216054014 0.024006002 0.16204051 0.597899475
TEX 0.177731674 0.015905947 0.14868603 0.657676349
TOR 0.203240059 0.013254786 0.162739323 0.620765832
WSN 0.191019244 0.020669993 0.14468995 0.643620813'), header = TRUE)
library(ggplot2)
library(reshape2)
hw <- theme_gray() + theme(
strip.background=element_rect(fill=rgb(.9,.95,1),
colour=gray(.5), size=.2),
panel.border=element_rect(fill=FALSE,colour=gray(.70)),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank(),
panel.spacing.x = unit(0.10,"cm"),
panel.spacing.y = unit(0.05,"cm"),
axis.ticks=element_blank(),
axis.text=element_text(colour="black"),
axis.text.y=element_text(margin=margin(0,0,0,3)),
axis.text.x=element_text(margin=margin(-1,0,3,0))
)
ord <- with(df,order(Singles,Doubles,Triples,HR))
dfOrd <- df[ord,]
tmp <- as.character(dfOrd$Team)
dfOrd$Team <- factor(tmp,rev(tmp))
tmp <- as.character(dfOrd$Team)
dfOrd$Team <- factor(tmp,rev(tmp))
colnames(dfOrd)
dfLong <- melt(dfOrd[,-c(0,4)],
value.name="Percent",
variable.name="Achievement")
head(dfLong)
tail(dfLong)
ggplot(dfLong,aes(x=Team,y=Percent,fill=Achievement)) +
geom_bar(stat="identity",alpha=I(1),color=gray(.2),width=.75,size=.4)+
coord_flip() + hw +
theme(legend.position="top",
axis.text.y=element_text(size = rel(.8)))+
labs(fill="", y="Percent",
title="Title")
library(ggplot2)
library(reshape2)
library(dplyr)
library(scales)
df %>% melt(id.var="Team") %>%
mutate(variable = factor(variable, levels=rev(c("Singles","Doubles","Triples","HR"))),
Team = factor(Team, levels=df$Team[order(df$Singles)])) %>%
ggplot(aes(Team, value, fill=variable)) +
geom_bar(stat="identity") +
coord_flip() +
scale_y_continuous(labels=percent) +
theme_classic() +
theme(legend.position="bottom",
axis.title.x=element_blank()) +
guides(fill=guide_legend(reverse=TRUE)) +
labs(fill="", y="")
我正在尝试制作一个堆积条形图,显示每支球队的单打、双打、三打和本垒打的百分比。但是,正如您从所附图片中看到的那样,我一直得到一个奇怪的结果。我想我在融化步骤上出错了,因为我不确定如何使用该功能。该图,如果做得正确,每行应该有四个堆叠条,每行所有四个条的总长度等于一个。请注意,您必须滚动才能查看更多代码。
如果能帮助解决此问题,我们将不胜感激。
df <- read.table(textConnection(
'Team Doubles Triples HR Singles
ARI 0.192697769 0.037863421 0.128465179 0.640973631
ATL 0.21011396 0.019230769 0.086894587 0.683760684
BAL 0.187544232 0.004246285 0.179051663 0.62915782
BOS 0.214643304 0.015644556 0.130162703 0.639549437
CHC 0.2079489 0.021291696 0.141234918 0.629524485
CHW 0.193977591 0.023109244 0.117647059 0.665266106
CIN 0.19743407 0.023521026 0.116892373 0.66215253
CLE 0.214634146 0.020209059 0.128919861 0.636236934
COL 0.205958549 0.030440415 0.132124352 0.631476684
DET 0.170731707 0.020325203 0.14295393 0.66598916
HOU 0.212874909 0.021214338 0.144842721 0.621068032
KCR 0.182068966 0.022758621 0.10137931 0.693793103
LAA 0.19787234 0.014184397 0.110638298 0.677304965
LAD 0.197674419 0.015261628 0.137354651 0.649709302
MIA 0.17739726 0.028767123 0.087671233 0.706164384
MIL 0.191685912 0.014626636 0.149345651 0.644341801
MIN 0.204400284 0.024840312 0.141944642 0.628814762
NYM 0.178837556 0.014157973 0.162444113 0.644560358
NYY 0.177793904 0.014513788 0.132801161 0.674891147
OAK 0.199704142 0.015532544 0.125 0.659763314
PHI 0.177011494 0.026819923 0.123371648 0.672796935
PIT 0.194249649 0.022440393 0.107293128 0.67601683
SDP 0.201568627 0.020392157 0.138823529 0.639215686
SEA 0.173582296 0.01175657 0.154218534 0.6604426
SFG 0.194850383 0.037578288 0.090466249 0.67710508
STL 0.21130742 0.022614841 0.159010601 0.607067138
TBR 0.216054014 0.024006002 0.16204051 0.597899475
TEX 0.177731674 0.015905947 0.14868603 0.657676349
TOR 0.203240059 0.013254786 0.162739323 0.620765832
WSN 0.191019244 0.020669993 0.14468995 0.643620813'), header = TRUE)
library(ggplot2)
library(reshape2)
hw <- theme_gray() + theme(
strip.background=element_rect(fill=rgb(.9,.95,1),
colour=gray(.5), size=.2),
panel.border=element_rect(fill=FALSE,colour=gray(.70)),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank(),
panel.spacing.x = unit(0.10,"cm"),
panel.spacing.y = unit(0.05,"cm"),
axis.ticks=element_blank(),
axis.text=element_text(colour="black"),
axis.text.y=element_text(margin=margin(0,0,0,3)),
axis.text.x=element_text(margin=margin(-1,0,3,0))
)
ord <- with(df,order(Singles,Doubles,Triples,HR))
dfOrd <- df[ord,]
tmp <- as.character(dfOrd$Team)
dfOrd$Team <- factor(tmp,rev(tmp))
tmp <- as.character(dfOrd$Team)
dfOrd$Team <- factor(tmp,rev(tmp))
colnames(dfOrd)
dfLong <- melt(dfOrd[,-c(0,4)],
value.name="Percent",
variable.name="Achievement")
head(dfLong)
tail(dfLong)
ggplot(dfLong,aes(x=Team,y=Percent,fill=Achievement)) +
geom_bar(stat="identity",alpha=I(1),color=gray(.2),width=.75,size=.4)+
coord_flip() + hw +
theme(legend.position="top",
axis.text.y=element_text(size = rel(.8)))+
labs(fill="", y="Percent",
title="Title")
library(ggplot2)
library(reshape2)
library(dplyr)
library(scales)
df %>% melt(id.var="Team") %>%
mutate(variable = factor(variable, levels=rev(c("Singles","Doubles","Triples","HR"))),
Team = factor(Team, levels=df$Team[order(df$Singles)])) %>%
ggplot(aes(Team, value, fill=variable)) +
geom_bar(stat="identity") +
coord_flip() +
scale_y_continuous(labels=percent) +
theme_classic() +
theme(legend.position="bottom",
axis.title.x=element_blank()) +
guides(fill=guide_legend(reverse=TRUE)) +
labs(fill="", y="")