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))
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 this dataframe
bat=rep(c("A", "B", "C", "D"),3)
loc=c(rep("ab",4), rep("te",4), rep("po", 4))
value=c(17,8,14,20,2,9,11,18,5,17,7,14)
tot=rep(c(30,45,36,58),3)
say=rep()
dt=data_frame(bat, loc, value, tot) %>% mutate(pct=value/tot*100) %>% mutate(se=sqrt(((pct*(100-pct))/(tot)))) %>% mutate(value2=tot-value)
> dt
# A tibble: 12 x 7
bat loc value tot pct se value2
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 A ab 17 30 56.7 9.05 13
2 B ab 8 45 17.8 5.70 37
3 C ab 14 36 38.9 8.12 22
4 D ab 20 58 34.5 6.24 38
5 A te 2 30 6.67 4.55 28
6 B te 9 45 20 5.96 36
7 C te 11 36 30.6 7.68 25
8 D te 18 58 31.0 6.07 40
9 A po 5 30 16.7 6.80 25
10 B po 17 45 37.8 7.23 28
11 C po 7 36 19.4 6.60 29
12 D po 14 58 24.1 5.62 44
I made fisher test between each pair of bat for each loc with this following code.
dt_ab=dt %>% filter(loc=="ab")
dt_po=dt %>% filter(loc=="po")
dt_te=dt %>% filter(loc=="te")
# ab
idx = t(combn(seq_along(dt_ab$bat),2))
res_ab = lapply(1:nrow(idx),function(i){
test = fisher.test(dt_ab[idx[i,],c("value","value2")])
data.frame(
group1 = dt_ab$bat[idx[i,1]],
group2 = dt_ab$bat[idx[i,2]],
loc=dt_ab$loc[idx[i,1]],
odds_ratio = as.numeric(test$estimate),
p = as.numeric(test$p.value)
)
})
res_ab = do.call(rbind,res_ab)
res_ab
# po
idx = t(combn(seq_along(dt_po$bat),2))
res_po = lapply(1:nrow(idx),function(i){
test = fisher.test(dt_po[idx[i,],c("value","value2")])
data.frame(
group1 = dt_po$bat[idx[i,1]],
group2 = dt_po$bat[idx[i,2]],
loc=dt_po$loc[idx[i,1]],
odds_ratio = as.numeric(test$estimate),
p = as.numeric(test$p.value)
)
})
res_po = do.call(rbind,res_po)
res_po
# te
idx = t(combn(seq_along(dt_te$bat),2))
res_te = lapply(1:nrow(idx),function(i){
test = fisher.test(dt_te[idx[i,],c("value","value2")])
data.frame(
group1 = dt_te$bat[idx[i,1]],
group2 = dt_te$bat[idx[i,2]],
loc=dt_te$loc[idx[i,1]],
odds_ratio = as.numeric(test$estimate),
p = as.numeric(test$p.value)
)
})
res_te= do.call(rbind,res_te)
res_te
res=NULL
res=rbind(res_ab, res_po, res_te)
res
group1 group2 loc odds_ratio p
1 A B ab 5.8817685 0.0009288055
2 A C ab 2.0321378 0.2157927844
3 A D ab 2.4578341 0.0678462682
4 B C ab 0.3445393 0.0451456088
5 B D ab 0.4143084 0.0750566107
6 C D ab 1.2066231 0.6665182345
7 A B po 0.3341536 0.0700086821
8 A C po 0.8309230 1.0000000000
9 A D po 0.6317547 0.5859688210
10 B C po 2.4870606 0.0896596469
11 B D po 1.8959538 0.1934540389
12 C D po 0.7608147 0.7994419705
13 A B te 0.2899040 0.1823689517
14 A C te 0.1664441 0.0270616991
15 A D te 0.1614577 0.0141330017
16 B C te 0.5722244 0.3084931936
17 B D te 0.5586957 0.2609751992
18 C D te 0.9780082 1.0000000000
Now I would like to add manually the pvalue and the odd ratio for each pair on the following barplot
I saw we can use the stat_pvalue_manual function to do that, but I have some difficulties to use it. Please, someone can have look on my code and tell me what is wrong. Below is the code and the plot I obtained.
bp_tab_pct<- ggplot(dt, aes(x=loc, y=pct, fill=cat))+
geom_bar(stat = "identity",position="dodge") +
scale_fill_manual(values = c("A"= "#5977FF", "B"="#FF7F50", "C"= "#00CED1", "D" = "#FFFF33"))+
theme(plot.title = element_text(color="black", size=15, face="bold.italic")) +
labs(fill = "") +
ylim(c(0,100))+
ggtitle(label = "")+
geom_text(aes(y = pct+se+2, label = paste(round(pct,2),"%", "\n","(",value,"/",tot,")")), color = "black",size=5, position = position_dodge(width = 0.9))+
theme(plot.title = element_text(color="black", size=15, face="bold.italic"))+
theme(axis.text.x = element_text(size=16), axis.text.y = element_text(size=14))+
theme(axis.title.y = element_text(size=16), axis.title.x = element_blank(),) +
geom_errorbar(aes(ymin=pct-se, ymax=pct+se), width=.2,
position=position_dodge(.9)) +
stat_pvalue_manual(res, y.position = 35, step.increase = 0.1,
label = "p", inherit.aes = FALSE)
enter image description here
I also tried with facet insted of group but I still don't have the plot I want.
It seems that the stat_pvalue_manual function considere only the 10 first lines and I don't know why.
bp_tab_pct<- ggplot(dt, aes(x=bat, y=pct, fill=bat))+
facet_wrap(~loc) +
geom_bar(stat = "identity",position="dodge") +
scale_fill_manual(values = c("A"= "#5977FF", "B"="#FF7F50", "C"= "#00CED1", "D" = "#FFFF33"))+
theme(plot.title = element_text(color="black", size=15, face="bold.italic")) +
labs(fill = "") +
ylim(c(0,100))+
ggtitle(label = "")+
geom_text(aes(y = pct+se+2, label = paste(round(pct,2),"%", "\n","(",value,"/",tot,")")), color = "black",size=5, position = position_dodge(width = 0.9))+
theme(plot.title = element_text(color="black", size=15, face="bold.italic"))+
theme(axis.text.x = element_text(size=16), axis.text.y = element_text(size=14))+
theme(axis.title.y = element_text(size=16), axis.title.x = element_blank(),) +
geom_errorbar(aes(ymin=pct-se, ymax=pct+se), width=.2,
position=position_dodge(.9)) +
stat_pvalue_manual(res, y.position = 35, step.increase = 0.1,
label = "p", inherit.aes = FALSE)
enter image description here
I understood the problem. It was du to the ylim masking the highest values on the y axis. Now I would like to be able to adjust for the y position of each pvalue as I did for the pct indicated up to the bar but I will post another question for that
enter image description here
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:
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!