Hi would to create a barplot like this
but the bars should by filled by the values of this plot, leaving the rest in a color like gray or black:
To produce the barplots I used:
> table
Var1 Freq
1 H3K27ac 72
2 H3K27me3 72
3 H3K36me3 72
4 H3K4me1 72
5 H3K4me2 66
6 H3K4me3 72
7 H3K9ac 66
8 H3K9me3 71
> table_filt
Var1 Freq
1 H3K27ac 31
2 H3K27me3 72
3 H3K36me3 0
4 H3K4me1 71
5 H3K4me2 66
6 H3K4me3 72
7 H3K9ac 60
8 H3K9me3 1
and the code is:
table%>%
ggplot(aes(Var1, Freq, fill = Var1)) +
geom_col() +
scale_fill_manual(values = colours)
table_filt%>%
ggplot(aes(Var1, Freq, fill = Var1)) +
geom_col() +
scale_fill_manual(values = colours)
The colour vector is:
colours
H3K27ac H3K27me3 H3K36me3 H3K4me1 H3K4me2 H3K4me3 H3K9ac H3K9me3
"mediumvioletred" "#E69F00" "#56B4E9" "#009E73" "#F0E442" "#0072B2" "firebrick4" "aquamarine"
I appreciate any suggestion.
Are you looking for something like this?
library(ggplot2)
ggplot(table, aes(Var1, Freq)) +
geom_col(fill = "gray75") +
geom_col(data = table_filt, aes(fill = Var1)) +
scale_fill_brewer(palette = "Set1")
Related
hello I have tried to graph the following data
I have tried to graph the following time series
fecha importaciones
1 Ene\n1994 171.0
2 Feb\n1994 170.7
3 Mar\n1994 183.7
4 Abr\n1994 214.6
5 May\n1994 227.2
6 Jun\n1994 221.1
7 Jul\n1994 216.4
8 Ago\n1994 235.3
9 Sep\n1994 227.0
10 Oct\n1994 216.0
11 Nov\n1994 221.5
12 Dic\n1994 270.9
13 Ene\n1995 250.4
14 Feb\n1995 259.6
15 Mar\n1995 258.2
16 Abr\n1995 232.9
17 May\n1995 335.0
18 Jun\n1995 295.2
19 Jul\n1995 302.5
20 Ago\n1995 283.3
21 Sep\n1995 264.4
22 Oct\n1995 277.6
23 Nov\n1995 289.1
24 Dic\n1995 280.5
25 Ene\n1996 252.4
26 Feb\n1996 250.1
.
.
.
320 Ago\n2020 794.6
321 Sep\n2020 938.2
322 Oct\n2020 966.3
323 Nov\n2020 958.9
324 Dic\n2020 1059.2
325 Ene\n2021 1056.2
326 Feb\n2021 982.5
I graph it with office cal
but trying to plot it in R with ggplot
ggplot(datos, aes(x = fecha, y = importaciones)) +
geom_line(size = 1) +
scale_color_manual(values=c("#00AFBB", "#E7B800"))+
theme_minimal()
I have tried to graph with all the possible steps but it does not fit me in a correct way for someone to guide me
Change the x-axis to date class.
library(ggplot2)
df$fecha <- lubridate::dmy(paste0(1, df$fecha))
ggplot(datos, aes(x = fecha, y = importaciones, group = 1)) +
geom_line(size = 1) +
scale_color_manual(values=c("#00AFBB", "#E7B800"))+
theme_minimal()
You can use scale_x_date to change the breaks and display format of dates on x-axis.
here is the data example:
S P C P_int C_int
10 20 164 72 64
20 550 709 92 89
30 142 192 97 96
40 45 61 99 98
50 12 20 99 99
60 5 6 99 99
70 2 2 99 99
80 4 1 99 99
90 1 0 10 99
100 0 1 10 99
Let's say i have a dataframe called df, the aim is to have a bar chart using variables P and C, with an line chart overlayed using sum of variables P_int and C_int. Currently I have these lines of codes to create the bar chart:
final <- df %>% tidyr::gather(type, value, c(`P`, `C`))
ggplot(final, aes(S))+
geom_bar(aes(y=value, fill=type), stat="identity", position="dodge")
The thing I can't figure out is hot to plot the sum of variables P_int and C_int as a line chart overlayed on the above plot with a second Y axis. Would appreciate any help.
Do you need something like this ?
library(ggplot2)
library(dplyr)
ggplot(final, aes(S))+
geom_bar(aes(y=value, fill=type), stat="identity", position="dodge") +
geom_line(data = final %>%
group_by(S) %>%
summarise(total = sum(P_int + C_int)),
aes(y = total), color = 'blue') +
scale_y_continuous(sec.axis = sec_axis(~./1)) +
theme_classic()
I have kept the scale of secondary y-axis same as primary y-axis since they are in the same range but you might need to adjust it in according to your real data.
I used ggplot2 to create the following barplot. However, I would like to add stars to show the significancy between the dark and light for the same treatment. For instance SW.5 treatment.
calen_per<- read.table("sadek calen percent.csv", sep=";",header = TRUE)
calen_per
DW SW.1 SW.2 SW.3 SW.4 SW.5 LVD
70 85 80 70 75 84 Light (79.5a)
75 90 85 77 72 86 Light (79.5a)
78 85 80 75 75 90 Light (79.5a)
72 70 74 65 70 70 Dark (70.8b)
75 72 70 70 72 75 Dark (70.8b)
70 75 70 70 65 70 Dark (70.8b)
cal_per <- melt(calen_per,id="LVD")
g8<-ggplot(cal_per, aes(x=variable, y=value, fill=as.factor(LVD)))
+ stat_summary(fun.y=mean,geom="bar",position=position_dodge(),
colour="black",width=.7,size=.5)+
stat_summary(fun.ymin=min,fun.ymax=max,geom="errorbar",
color="black",position=position_dodge(.7), width=.2) +
stat_summary(geom = 'text', fun.y = max, position = position_dodge(.7),
label = c("B","a","A","a","AB","a", "B","a","B","a","A","a"), vjust = -0.5)+
scale_fill_manual("marigold",
values = c("Light (79.5a)" = "white", "Dark (70.8b)" = "grey")) +
xlab("treatments")+ylab("percentage") +
theme(panel.background = element_rect(fill = 'white', colour = 'black'))+
ylim(0,120)
How can I do this?
I am trying to position two bars at the same position on the x-axis and seperated out by colour (almost as if stacking).
However, instead of stacking I want the bar simply inside the other bar - with the smallest Y-value being visable inside the bar with the highest Y-value.
I can get this to work to some extent - but the issue is that one Y-value is not consistently higher across one of the two factors. This leads to bars being 'lost' within a bar with a higher Y-value.
Here is a subset of my dataset and the current ggplot code:
condition hours expression freq_genes
1 tofde 9 up 27
2 tofde 12 up 92
3 tofde 15 up 628
17 tofde 9 down 0
18 tofde 12 down 1
19 tofde 15 down 0
33 tofp 9 up 2462
34 tofp 12 up 786
35 tofp 15 up 298
49 tofp 9 down 651
50 tofp 12 down 982
51 tofp 15 down 1034
65 tos 0 up 27
66 tos 3 up 123
67 tos 6 up 752
81 tos 0 down 1
82 tos 3 down 98
83 tos 6 down 594
sf_plot <- ggplot(data = gene_freq,
aes(x = hours,
y = freq_genes,
group = condition,
fill = factor(expression,
labels=c("Down",
"Up"))))
sf_plot <- sf_plot + labs(fill="Expression")
sf_plot <- sf_plot + geom_bar(stat = "identity",
width = 2.5,
position = "dodge")
sf_plot <- sf_plot + scale_fill_manual(values=c("#9ecae1",
"#3182bd"))
sf_plot <- sf_plot + xlab("Time (Hours)")
sf_plot <- sf_plot + scale_x_continuous(breaks =
seq(min(gene_freq$freq_genes),
max(gene_freq$freq_genes),
by = 3))
sf_plot <- sf_plot + ylab("Gene Frequency")
sf_plot <- sf_plot + facet_grid(. ~ condition, scales = "free")
sf_plot <- sf_plot + theme_bw()
sf_plot <- sf_plot + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
sf_plot <- sf_plot + theme(axis.text.x = element_text(angle = 90))
# Print plot
sf_plot
You can add alpha = 0.5 to your geom_bar() statement to make the bars transparent. This will allow both bars to be seen. Adding that alpha statement and nothing else will produce what you're looking for, to make both overlaid bars visible. The colors, however, make seeing the two different bars challenging.
Another (and maybe better) option is to change the order in which the plot is created. If I recall correctly, ggplot will plot the bars in alphabetical or numeric or factor-level order. Here, your expression values are c("Down", "Up") and "Down" is being plotted first. If you force "Up" to be plotted first, you could resolve this, too.
library(dplyr)
library(ggplot2)
dat <-
read.table(text = "condition hours expression freq_genes
1 tofde 9 up 27
2 tofde 12 up 92
3 tofde 15 up 628
17 tofde 9 down 0
18 tofde 12 down 1
19 tofde 15 down 0
33 tofp 9 up 2462
34 tofp 12 up 786
35 tofp 15 up 298
49 tofp 9 down 651
50 tofp 12 down 982
51 tofp 15 down 1034
65 tos 0 up 27
66 tos 3 up 123
67 tos 6 up 752
81 tos 0 down 1
82 tos 3 down 98
83 tos 6 down 594") %>%
mutate(expression2 = ifelse(expression == "up", 1, 2))
dat %>%
ggplot(aes(x = hours, y = freq_genes, group = condition,
fill = factor(expression2, labels=c("Up", "Down")))) +
labs(fill="Expression") +
geom_bar(stat = "identity", position = "dodge", width = 2.5, alpha = 0.5) +
scale_fill_manual(values=c("#9ecae1", "#3182bd")) +
xlab("Time (Hours)") +
scale_x_continuous(breaks = seq(min(dat$freq_genes),
max(dat$freq_genes),
by = 3)) +
ylab("Gene Frequency") +
facet_grid(. ~ condition, scales = "free") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "bottom",
axis.text.x = element_text(angle = 90))
Here, I've created a new column called expression2 that is just a numeric version of expression. I changed the fill variable in aes() to match with those new labels. I left the colors in scale_fill_manual() the same as in your original statement and kept the alpha value. "Down" is being plotted on top of "Up" but in keeping the same colors with the alpha value, both bars are easier to see. You can play with the legend to display "Down" before "Up" if that's necessary.
Note that providing machine readable data goes a long way in allowing others to help you out. Consider using dput() to output your data next time rather than pasting it in. Also note that you can "chain" together ggplot() statements with a +. This makes code much more compact and easier to read.
I am new to stackoverflow so please be forgiving, if the way of asking my question can be improved (in that case I'm happy if you let me know how).
I am using the following code in ggplot2 to produce a grouped boxplot combined with a dotplot (unfortunately I cannot post an image yet (no reputation)):
ggplot(data24, aes(x=intensity, y=percacc, fill=group)) +
geom_boxplot(position=position_dodge(1), notch=T,
outlier.colour = NA, width = .7, alpha=0.2) +
geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 3,
position=position_dodge(1), dotsize=0.5, alpha=1) +
stat_summary(fun.y=mean, geom="point", shape=23, size=5,
position=position_dodge(0.3)) +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.1,
position=position_dodge(0.3), size=1.2)+
scale_y_continuous(limits=c(0,100)) +
scale_fill_discrete(name="Group")
My questions are:
How can use different colors for different elements? I tried to add color/fill commands within geom_boxplot() and geom_dotplot, but this doesn't work: e.g., if I add fill="green" to geom_dotplot(), all points become green and centered between the boxplots. How can I rewrite the code to get
white fill for all boxplots
blue fill and black line for dots of group 1
green fill and black line for dots of group 2
black fill for all mean diamonds
How can pull the categories of "intensity" (i.e. the three pairs of boxplots) further apart from each other?
How can I display the dotplots next to the boxplots and the mean+CI in the boxplot?
Why does my coordination system still go from <0 to >100, even if I defined the y-axis to go from 0 to 100?
Thank you!
Edit 180722
Thank you for your comments. The data head is:
id intensity AQ_sum group acc percacc
1 54 40 11 COMP 5 20.83333
2 54 60 11 COMP 18 75.00000
3 54 80 11 COMP 24 100.00000
4 55 40 12 COMP 9 37.50000
5 55 60 12 COMP 22 91.66667
6 55 80 12 COMP 24 100.00000
7 58 40 10 COMP 8 33.33333
8 58 60 10 COMP 22 91.66667
9 58 80 10 COMP 23 95.83333
10 59 40 6 COMP 19 79.16667
11 59 60 6 COMP 24 100.00000
12 59 80 6 COMP 24 100.00000
13 60 40 9 COMP 10 41.66667
14 60 60 9 COMP 23 95.83333
15 60 80 9 COMP 22 91.66667
16 61 40 13 COMP 4 16.66667
17 61 60 13 COMP 19 79.16667
18 61 80 13 COMP 24 100.00000
19 62 40 12 COMP 16 66.66667
20 62 60 12 COMP 23 95.83333
My updated code is
ggplot(data24, aes(x=intensity, y=percacc, fill=group)) +
geom_boxplot(position=position_dodge(0.8), notch=T,
outlier.colour = NA, width = .4, alpha=0.3) +
geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 3,
position=position_dodge(0.8), dotsize=0.4, alpha=1)+
scale_fill_manual(values=c("#999999", "#E69F00"), name="Group") +
stat_summary(fun.y=mean, geom="point", shape=23, size=3,
position=position_dodge(0.2)) +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.1,
position=position_dodge(0.2), size=0.5)+
scale_y_continuous(limits=c(0,103), expand = c(0, 0),
breaks=c(0,20,40,60,80,100), name="Percentage accuracy") +
scale_x_discrete(expand = c(0, 0.6), name="Degree of intensity (in percent)") +
labs(title="Accuracy by intensity and group\n") +
theme_light()+
theme(plot.title = element_text(face='bold', size=12, hjust = 0.5),
axis.title.x = element_text(size=10,hjust=0.5),
axis.title.y = element_text(size=10,vjust=1),
axis.text.x = element_text(size=10,color='black'),
axis.text.y = element_text(size=10, color='black'),
panel.grid.major.y = element_line(size = 0.3, linetype = "dotted", color="darkgrey"),
panel.grid.minor.y = element_line(size = 0.3, linetype = "dotted", color="darkgrey"),
panel.grid.major.x = element_blank(),
panel.border = element_blank(),
axis.line = element_line(size = 0.5, linetype = "solid", colour = "black")) +
ggsave("plotintensity.png", width = 10, height = 5)