ggplot: error bars do not appear when dodging - r

I'm having a horrible time getting errors bars to plot correctly. Is something involving the overlap function(dodging) causing trouble?
Data:
mean mean_b se se.1 seb seb.1 ID
1 0.52 0.20 0.137 0.137 0.015 0.015 1
2 0.17 0.20 0.062 0.062 0.016 0.016 2
3 0.46 0.60 0.078 0.078 0.006 0.006 3
4 0.34 0.11 0.134 0.134 0.005 0.005 4
5 0.22 0.10 0.066 0.066 0.004 0.004 5
6 0.62 0.14 0.083 0.083 0.003 0.003 6
7 0.11 0.29 0.133 0.133 0.065 0.065 7
8 0.51 0.44 0.113 0.113 0.026 0.026 8
9 0.41 0.50 0.082 0.082 0.009 0.009 9
# grab data for data A
df_m <- data[ , c(7, 1, 3, 4)]
df_m$comp <- "Initial Occupancy"
names(df_m) <- c("ID", "avg", "lower", "upper", "comp")
# grab data for data B
df_f <- data[ , c(7, 2, 5, 6)]
df_f$comp <- "Equilibrium Occupancy"
names(df_f) <- c("ID", "avg", "lower", "upper", "comp")
# bind the data together
df <- rbind(df_m, df_f)
# plot
ggplot(data = df, aes(x = ID, y = avg, ymin = lower, ymax = upper, colour = comp)) +
geom_point(position = position_dodge(width = 0.4)) +
geom_errorbar(position = position_dodge(width = 0.4), width = .3) +
coord_flip() +
scale_colour_manual(values = c("blue", "red")) +
theme_bw() +
theme(panel.grid.major.y = element_line(colour = "grey", linetype = "dashed"),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank())

data=read.csv()
# grab data for males
df_m <- data[ , c(12, 1, 3)]
df_m$comp <- "Initial Occupancy"
names(df_m) <- c("ID", "avg", "se", "comp")
df_m
# grab data for females
df_f <- data[ , c(12, 2, 5)]
df_f$comp <- "Equilibrium Occupancy"
names(df_f) <- c("ID", "avg", "se", "comp")
df_f
# bind the data together
df <- rbind(df_m, df_f)
# plot
ggplot(data = df, aes(x = ID, y = avg, ymin = avg-se, ymax = avg+se, colour = comp)) +
geom_point(position = position_dodge(width = 0.4),pch=21) +
geom_errorbar( position = position_dodge(width = 0.4), width = .3) +
coord_flip() +
scale_colour_manual(values = c("blue", "red")) +
#theme_classic()
theme_bw() +
theme(panel.grid.major.y = element_line(colour = "grey", linetype = "dashed"),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank())
Thank you jlhoward!

Related

overlapping plot with emmeans

I have the following emmeans tables:
emm_1
$emmeans
Order rate SE df asymp.LCL asymp.UCL
1 19.3 1.51 Inf 16.5 22.5
2 26.0 2.33 Inf 21.8 31.0
emm_2
$emmeans
Order rate SE df asymp.LCL asymp.UCL
1 25.6 1.62 Inf 22.6 28.9
2 18.8 2.34 Inf 14.8 24.0
And I'm trying to plot them both together in the same plot:
plot(emm_1,col="steelblue4") + theme_bw() +
labs(title = "Choice1",
x = "Estimated marginal mean",
y = "Order") + theme(plot.title = element_text(hjust = 0.5)) +
scale_x_continuous(breaks = seq(0, 33,5), limits =c(0,33))
par(new=TRUE)
plot(emm_2,col="green") + theme_bw() +
labs(title = "Choice2",
x = "Estimated marginal mean",
y = "Order") + theme(plot.title = element_text(hjust = 0.5)) +
scale_x_continuous(breaks = seq(0, 33,5), limits =c(0,33))
This runs, although only the second plot is plotted. Is it possible to do this? What do I need to fix?

Creating a bar graph with several variables in the x axis

I have the following dataset (graph_data):
# A tibble: 18 x 7
construction phase group mean se se_top se_bottom
<chr> <fct> <fct> <dbl> <dbl> <dbl> <dbl>
1 hacer pre-test heritage 7.67 3.67 11.3 4
2 hacer treatment heritage 15.5 3.00 18.5 12.5
3 hacer post-test heritage 9.83 4.25 14.1 5.58
4 acc pre-test heritage 0.166 0.166 0.332 0
5 acc treatment heritage 4.33 2.67 7.00 1.67
6 acc post-test heritage 0.166 0.166 0.332 0
7 spe pre-test heritage 2.33 1.36 3.69 0.975
8 spe treatment heritage 6.67 2.69 9.36 3.98
9 spe post-test heritage 0.833 0.477 1.31 0.356
10 hacer pre-test monolingual 1 0.707 1.71 0.293
11 hacer treatment monolingual 1 0.577 1.58 0.423
12 hacer post-test monolingual 0.25 0.25 0.5 0
13 acc pre-test monolingual 0 0 0 0
14 acc treatment monolingual 1 0.577 1.58 0.423
15 acc post-test monolingual 0 0 0 0
16 spe pre-test monolingual 4 3.37 7.37 0.634
17 spe treatment monolingual 15.8 2.36 18.1 13.4
18 spe post-test monolingual 3.5 3.18 6.68 0.325
I want to create a bar graph using ggplot2 with the following conditions:
y axis: mean
x axis: phase + construction
facet: group
error bars: standard error (se_top, se_bottom)
I have used this code:
graph_data %>%
ggplot(aes(graph_data, x=phase, y=mean, fill =phase)) +
geom_bar(stat = "identity", color = "black", position = "dodge") +
scale_y_continuous(limits = c(0,20)) +
labs(x = "Phase", y = "Average number of targets produced") +
facet_wrap( ~ group) +
geom_errorbar(aes(ymin= se_bottom, ymax = se_top), width=.2,
position=position_dodge(.9)) +
theme(text = element_text(size=20)) +
theme_classic() + scale_fill_manual(values=c("#90EE90", "#3CB371", "#2E8B57")) +
theme(axis.text=element_text(size=16),
axis.title=element_text(size=15,face="bold"),
axis.text.x = element_text(angle=45, hjust = 1),
legend.position = "none")
However, in the graph that I get, columns are stacked on top of each other, even though I have used position = "dodge" in my code:
What should I change in order to get the graph I want?
Maybe this can be useful:
library(ggplot2)
#Code
graph_data %>%
ggplot(aes(graph_data, x=interaction(phase,construction), y=mean, fill =phase,group=group)) +
geom_bar(stat = "identity", color = "black", position = position_dodge(0.9)) +
scale_y_continuous(limits = c(0,20)) +
labs(x = "Phase", y = "Average number of targets produced") +
facet_wrap( ~ group) +
geom_errorbar(aes(ymin= se_bottom, ymax = se_top), width=.2,
position=position_dodge(.9)) +
theme(text = element_text(size=20)) +
theme_classic() + scale_fill_manual(values=c("#90EE90", "#3CB371", "#2E8B57")) +
theme(axis.text=element_text(size=16),
axis.title=element_text(size=15,face="bold"),
axis.text.x = element_text(angle=45, hjust = 1),
legend.position = "none")
Output:
Update: In order to get some order, try this:
#Code 2
graph_data %>%
mutate(phase=factor(phase,levels = c('pre-test','treatment','post-test'),
ordered = T)) %>%
arrange(phase) %>%
mutate(conc=paste(as.character(phase),construction),
conc=factor(conc,levels = unique(conc),ordered = T)) %>%
ggplot(aes(graph_data, x=conc, y=mean, fill =phase,group=group)) +
geom_bar(stat = "identity", color = "black", position = position_dodge(0.9)) +
scale_y_continuous(limits = c(0,20)) +
labs(x = "Phase", y = "Average number of targets produced") +
facet_wrap( ~ group) +
geom_errorbar(aes(ymin= se_bottom, ymax = se_top), width=.2,
position=position_dodge(.9)) +
theme(text = element_text(size=20)) +
theme_classic() + scale_fill_manual(values=c("#90EE90", "#3CB371", "#2E8B57")) +
theme(axis.text=element_text(size=16),
axis.title=element_text(size=15,face="bold"),
axis.text.x = element_text(angle=45, hjust = 1),
legend.position = "none")
Output:

How to make ggplot2 exclude zero values in this plot?

I have the following R codes running in RStudio, with the output also shown below:
df2 %>%
ggplot(aes(
x = JANUARY,
y = value,
fill = JANUARY,
group = year
)) +
geom_col(
position = position_dodge(.65),
width = .5
) +
geom_text(aes(
y = value + max(value) * .03,
label = round(value * 100) %>% str_c('%')
),
position = position_dodge(.65)
) +
geom_text(aes(
y = y_pos,
label = str_remove(year, 'X')
),
color = 'white',
angle = 90,
fontface = 'bold',
position = position_dodge(0.65)
) +
scale_y_continuous(
breaks = seq(0, .9, .1),
labels = function(x) round(x * 100) %>% str_c('%')
) +
scale_fill_manual(values = c(
rgb(47, 85, 151, maxColorValue = 255),
rgb(84, 130, 53, maxColorValue = 255),
rgb(244, 177, 131, maxColorValue = 255),
rgb(112, 48, 160, maxColorValue = 255),
rgb(90, 48, 100, maxColorValue = 255)
)) +
theme(
plot.title = element_text(hjust = .5),
panel.background = element_blank(),
panel.grid.major.y = element_line(color = rgb(.9, .9, .9)),
axis.ticks = element_blank(),
legend.position = 'none'
) +
xlab('') +
ylab('') +
ggtitle('Month of JANUARY (as at 01 January)')
Output is:
As you can see, the value "0%" under "D-Final" is causing the labels inside the bars to disappear below the x-axis.
I want to remove the "0%" and get the labels back into position inside the bars. How can I modify my codes to achieve this?
Data (df2) added:
JANUARY year value y_pos
<fct> <chr> <dbl> <dbl>
1 D-150 X2016 0.26 0.12
2 D-90 X2016 0.49 0.21
3 D-60 X2016 0.63 0.265
4 D-30 X2016 0.73 0.325
5 D-Final X2016 0.81 0
6 D-150 X2017 0.28 0.12
7 D-90 X2017 0.5 0.21
8 D-60 X2017 0.64 0.265
9 D-30 X2017 0.77 0.325
10 D-Final X2017 0.82 0
11 D-150 X2018 0.33 0.12
12 D-90 X2018 0.51 0.21
13 D-60 X2018 0.62 0.265
14 D-30 X2018 0.77 0.325
15 D-Final X2018 0.78 0
16 D-150 X2019 0.24 0.12
17 D-90 X2019 0.42 0.21
18 D-60 X2019 0.53 0.265
19 D-30 X2019 0.65 0.325
20 D-Final X2019 0 0
It's not really about 0%, at least this point. Position of the labels is predefined and given by y_pos, so you may simply alter it yourself with, e.g.,
df2$y_pos[df2$JANUARY == "D-Final"] <- 0.4
As to remove 0%, the first line could be replaced by
df2 %>% filter(value > 0.01) %>%
This gives
Apparently y_pos was defined with
df2 %>% group_by(JANUARY) %>% mutate(y_pos = min(value) / 2)
Hence, as to avoid this issue, in this case (since all the other value by group are similar) you may instead use
df2 %>% group_by(JANUARY) %>% mutate(y_pos = max(value) / 2)
in the first line you can filter df2 to get all non-zero values using df2 %>% filter(value!=0)

R Row Labeled Plot for MLB Teams and a Few Baseball Statistical Categories

I am trying to make a graph similar to the picture provided. Rather than states, I would like the teams. Instead of "Basic or Above", "Proficient or Above", and "Advanced", I would like "BA", "OBP", "SLG", and "OPS", with the teams being listed based on "BA". Additionally, I would like alternating colors for the points, like in the picture. Here is what I have so far, but I am having difficulty with the parts in between ggplot and rowTheme. Note that you have to scroll to view more code.
Any help would be much appreciated.
df <- read.table(textConnection(
'Team BA OBP SLG OPS
ARI 0.261 0.32 0.432 0.752
ATL 0.255 0.321 0.384 0.705
BAL 0.256 0.317 0.443 0.76
BOS 0.282 0.348 0.461 0.81
CHC 0.256 0.343 0.429 0.772
CHW 0.257 0.317 0.41 0.727
CIN 0.256 0.316 0.408 0.724
CLE 0.262 0.329 0.43 0.759
COL 0.275 0.336 0.457 0.794
DET 0.267 0.331 0.438 0.769
HOU 0.247 0.319 0.417 0.735
KCR 0.261 0.312 0.4 0.712
LAA 0.26 0.322 0.405 0.726
LAD 0.249 0.319 0.409 0.728
MIA 0.263 0.322 0.394 0.716
MIL 0.244 0.322 0.407 0.729
MIN 0.251 0.316 0.421 0.738
NYM 0.246 0.316 0.417 0.733
NYY 0.252 0.315 0.405 0.72
OAK 0.246 0.304 0.395 0.699
PHI 0.24 0.301 0.384 0.685
PIT 0.257 0.332 0.402 0.734
SDP 0.235 0.299 0.39 0.689
SEA 0.259 0.326 0.43 0.756
SFG 0.258 0.329 0.398 0.728
STL 0.255 0.325 0.442 0.767
TBR 0.243 0.307 0.426 0.733
TEX 0.262 0.322 0.433 0.755
TOR 0.248 0.33 0.426 0.755
WSN 0.256 0.326 0.426 0.751'), header = TRUE)
library(ggplot2)
library(tidyr)
library(dplyr)
rowTheme <- theme_gray()+ theme(
plot.title=element_text(hjust=0.5),
plot.subtitle=element_text(hjust=0.5),
plot.caption=element_text(hjust=-.5),
strip.text.y = element_blank(),
strip.background=element_rect(fill=rgb(.9,.95,1),
colour=gray(.5), size=.2),
panel.border=element_rect(fill=FALSE,colour=gray(.75)),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank(),
panel.spacing.x = unit(0.07,"cm"),
panel.spacing.y = unit(0.07,"cm"),
axis.ticks=element_blank(),
axis.text=element_text(colour="black"),
axis.text.y=element_text(size=rel(.78),
margin=margin(0,0,0,3)),
axis.text.x=element_text(margin=margin(-1,0,3,0))
)
colName <- function(x){
ints= 1:length(x)
names(ints)=x
return(ints)
}
rowOrd <- with(df,
order(BA, OBP,
OPS, SLG, decreasing=TRUE))
colOrd <- c(1,5,4,3,2)
df2 <- df[rowOrd,colOrd]
head(df2[,c(1,2,3,4,5)])
windows(width=8, height=9)
df3 <-
(ggplot(df,aes(x=Percent,y=Team,fill=Row,group=Grp))
+ labs(title= "Title",
x="Percent", y="Teams")
+ geom_point(shape=21,size=3)
+ scale_fill_manual(values=rowColor, guide=FALSE)
+ facet_grid(Grp ~ Achievement, scale="free",space="free_y")
+ rowTheme
+ theme(axis.text.y=element_text(size=rel(.78),
face='bold'))
)
df3
How about:
library(reshape)
library(ggplot2)
df$Team2 <- reorder(df$Team, df$BA)
dfmelt <- melt(df, id = c("Team", "Team2") )
p <- ggplot(dfmelt, aes(value, Team2))
p + geom_point(aes(colour=Team2)) + facet_wrap(~ variable, scales = "free", ncol = 4)+ geom_blank(data=dfmelt)
Is this what you're looking for, more or less?
library(dplyr)
df$Team <- reorder(as.factor(df$Team), df$BA)
row.names(df) <- NULL
dfx <- gather(df, group, data, BA, OBP, SLG, OPS)
dfx$data <- dfx$data*100
plot <- ggplot(dfx, aes(x = data, y = Team, group = group, fill = Team)) +
labs(title = "Title", x = "Percent", y = "Teams") +
geom_point(shape = 21, size = 3) +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
plot.caption = element_text(hjust = -0.5),
legend.position = "",
strip.text.y = element_blank(),
strip.background = element_rect(fill = rgb(.9,.95,1),
colour = gray(.5), size=.2),
panel.border = element_rect(fill = FALSE, colour=gray(.75)),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank(),
panel.spacing.x = unit(0.07,"cm"),
panel.spacing.y = unit(0.07,"cm"),
axis.ticks = element_blank(),
axis.text = element_text(colour = "black"),
axis.text.y = element_text(size = rel(.78), face = "bold",
margin = margin(0,0,0,3)),
axis.text.x = element_text(margin = margin(-1,0,3,0))) +
facet_grid(~group, scale = "free")
plot

ggplot chart, x=date, y= value, value

I have this testdata :
date cpu_user cpu_id test1 test2 test3 test4
1 1386716402 U U U U U 31
2 1386716702 0 0.06 99.95 0.02 91.93 29
3 1386717002 0.01 0.04 99.97 0.03 19.46 29
4 1386717302 0.01 0.05 99.96 0.04 92.54 29
5 1386717602 0 0.04 99.97 0.04 U 29
6 1386717902 0 0.05 99.96 0.02 99.86 29
I want for example a freqpoly chart with date at x and the other(cpu_uder, cpu_id, ....) at y. Have someone an idea?
Thanks and best Regards!
d <- read.table(text=readClipboard(), header=TRUE, stringsAsFactors = T,
na.strings = 'U')
df <- melt(d, id.var='date')
ggplot(aes(x=date, y=value), data = df) +
geom_bar(aes(fill = variable), stat = 'identity', position = 'dodge')
or
ggplot(aes(x=factor(date), y=value), data = df) +
geom_bar(stat = 'identity', position = 'dodge') +
facet_grid(variable~., scales = 'free_y', drop = F) +
theme(axis.text.x = element_text(angle = 45, vjust = 1.1, hjust = 1.05))

Resources