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:
Related
I'm trying to do a map with ggplot and geom_scatterpie function but I'm keep receiving this error: "Error: Discrete value supplied to continuous scale"
the code is the following:
ggplot() +
geom_tile(data = my_raster, aes(x = x, y = y, fill = values)) +
scale_fill_gradientn(colours = c("white", "white", "white", "white", "grey", "white", "white"), na.value = "white", guide="none") +
geom_sf(data = europe_cropped, size = 0.1, color = "black", fill = "white", alpha = 0.4) +
geom_scatterpie(aes(x = long, y = lat, r = 2),
cols = colnames(my_table[2:5], color = "black",
data = my_table) +
ylab("") +
xlab("") +
coord_sf() +
theme_few()
I've tried to run it without the geom_tile and scale_fill lines and it works but I need that raster as base of the map. What could be the problem the gives the error?
edit:
head(my_raster)
x
y
focal_max
-9.979089
64.98143
200
-9.965076
64.98143
200
-9.951063
64.98143
200
-9.937050
64.98143
200
-9.923037
64.98143
200
-9.909024
64.98143
200
head(my_table)
site
group1
group2
group3
group4
lat
long
site1
0.317
0
0.0448
0.280
47.7
5.08
site2
0.370
0
0.0359
0.319
47.8
5.07
site3
0.344
0
0.0269
0.233
47.8
5.12
site4
0.317
0
0.00896
0.342
47.8
5.06
site5
0.291
0
0.0538
0.373
47.8
5.04
site6
0.476
0
0.0179
0.490
47.7
5.12
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?
I created a stacked bar chart where the bars represent a percentage of the population. I would like to add labels to the 65+ category (or for all 3 categories if it is not possible to do it just for 1 category) showing the % value for each year. If I add geom_text(label = datm$value), the bars become extremely small because the labels represent absolute values instead of percentages. This is my code:
dat <- read.table(text = "2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
0-20 24.0 23.9 23.7 23.5 23.3 23.1 22.9 22.7 22.5 22.3 22.2
20-65 61.3 61.2 61.0 60.9 60.5 60.1 59.8 59.6 59.3 59.1 59.0
65+ 14.8 15.0 15.3 15.6 16.2 16.8 17.4 17.7 18.2 18.5 18.8", sep = " ", header = TRUE)
library(reshape)
datm <- melt(cbind(dat, ind = rownames(dat)), id.vars = c('ind'))
library(scales)
library(ggplot2)
ggplot(datm,aes(x = variable, y = value, fill = ind)) +
geom_bar(position = "fill",stat = "identity") +
scale_x_discrete(labels = c('2008', '2009', '2010', '2011', '2012', '2013',
'2014', '2015', '2016', '2017', '2018')) +
scale_y_continuous(labels = percent_format()) +
xlab('Year') +
ylab('% of population') +
ggtitle('Demographic trend in the Netherlands') +
scale_fill_manual(values = c("green", "blue", "darkgray"))
You can try this. Explanations in comments below:
library(dplyr)
# calculate percentage within each year
datm2 <- datm %>%
group_by(variable) %>%
mutate(p = value / sum(value)) %>%
ungroup()
> head(datm2)
# A tibble: 6 x 4
ind variable value p
<fct> <fct> <dbl> <dbl>
1 0-20 X2008 24 0.240
2 20-65 X2008 61.3 0.612
3 65+ X2008 14.8 0.148
4 0-20 X2009 23.9 0.239
5 20-65 X2009 61.2 0.611
6 65+ X2009 15 0.150
ggplot(datm2, aes(x = variable, y = value, fill = ind)) +
geom_col(position = "fill") + # geom_col is equivalent to geom_bar(stat = "identity")
geom_text(aes(label = scales::percent(p), # add layer for percentage values
alpha = ifelse(ind == "65+", 1, 0)), # only visible for 65+ category
position = position_fill(vjust = 0.5)) + # follow barplot's position
scale_x_discrete(labels = c('2008', '2009', '2010', '2011', '2012', '2013',
'2014', '2015', '2016', '2017', '2018')) +
scale_y_continuous(labels = percent_format()) +
scale_alpha_identity() +
xlab('Year') +
ylab('% of population') +
ggtitle('Demographic trend in the Netherlands') +
scale_fill_manual(values = c("green", "blue", "darkgray"))
I would like to change the linetype of one of my two lines in the plot, only making "line1" into a dashed one.
My plot:
My data looks like as bellow:
Year Sex value Rate Group
<dbl> <chr> <dbl> <dbl> <chr>
1 1912 Female 18 1.14 A
2 1912 Male 52 0.893 L
3 1913 Female 25 1.02 A
4 1913 Male 42 1.05 L
5 1914 Female 14 1.26 A
6 1914 Male 67 1.29 L
7 1915 Female 25 1.32 A
8 1915 Male 61 1.45 L
9 1916 Female 32 1.52 A
10 1916 Male 71 1.64 L
11 1917 Female 42 2.01 A
12 1917 Male 92 1.87 L
My code:
data %>% ggplot() +
geom_bar(aes(x = Year, y = value, fill= Sex), stat = "identity",
width=0.8,
alpha=0.8) +
geom_line(aes(x = Year, y = Rate * 100, colour= Group),
size = 1.0) +
scale_colour_manual(labels = c("line1","line2"),
values = c("red","blue"))+
scale_fill_manual(values = c("Female"="green","Male"="black"))+
guides(fill=guide_legend(title = "Number"),
color=guide_legend(title= "Ratio"))
Could anyone help? I tried for quite a while but failed. Thanks in advance.
As suggested in comments, and using the "name" term in each scale_*_manual to specify the legend names:
data %>%
ggplot() +
geom_bar(aes(x = Year, y = value, fill= Sex), stat = "identity",
width=0.8,
alpha=0.8) +
geom_line(aes(x = Year, y = Rate * 100,
colour = Group, linetype = Group),
size = 1.0) +
scale_colour_manual(name = "Ratio",
labels = c("line1","line2"),
values = c("red","blue"))+
scale_linetype_manual(name = "Ratio",
labels = c("line1","line2"),
values = c("dashed","solid"))+
scale_fill_manual(name = "Number",
values = c("Female"="green","Male"="black"))
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!