I'm making bar charts that show the difference between groups over separate periods, however I also want to shade out certain bars that don't meet certain criteria (i.e CV > 20 or Sample_size < 30).
Can someone please help me to do this? I've attached a picture as an example of what I'm looking for my output to look like (mocked up in excel), and some example data below.
Thanks a million!!
Example data:
structure(list(Group = c("A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"),
Perc = c(38, 38, 46, 27, 18, 39, 43, 27, 38, 40, 39, 39,
50, 32, 33, 41, 52, 32, 43, 43), Lower_CI = c(36, 36, 36,
15, 8, 31, 30, 14, 18, 23, 38, 37, 40, 21, 15, 31, 40, 17,
24, 27), Upper_CI = c(39, 40, 57, 39, 27, 47, 55, 40, 58,
56, 41, 41, 60, 44, 51, 51, 64, 47, 63, 60), CV = c(2, 2,
11, 12, 28, 11, 15, 6, 18, 13, 2, 2, 10, 19, 5, 13, 12, 23,
14, 20), Sample_size = c(30, 15, 140, 40, 69, 75, 56, 256,
45, 536, 4852, 785, 36, 48, 315, 120, 83, 15, 123, 45), Period = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
You can try a dplyr and ggplot2 pipeline. You can integrate a new value for Period using mutate(). After that you can also keep the same order for Group formating it as a factor. The plot you want is a dodged bar plot. Using ggplot2 functions you can reach that. Finally, move the axis using coord_flip(). The additional level created can be dropped using breaks inside scale_fill_manual(). Here the code:
library(ggplot2)
library(dplyr)
#Code
df %>%
mutate(Period=ifelse(CV>20 | Sample_size<30,3,Period),
Group=factor(Group,levels = rev(unique(Group)),ordered = T))%>%
ggplot(aes(x=Group,y=Perc,fill=factor(Period)))+
geom_bar(stat = 'identity',position = position_dodge(0.9))+
geom_errorbar(aes(x=Group,ymin=Lower_CI,ymax=Upper_CI,group=Period),
position = position_dodge(0.9))+
scale_fill_manual(values=c('cornflowerblue','orange','gray'),
breaks=c('1','2'))+
labs(fill='Period')+
theme_bw()+
coord_flip()
Output:
Some data used:
#Data
df <- structure(list(Group = c("A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"),
Perc = c(38, 38, 46, 27, 18, 39, 43, 27, 38, 40, 39, 39,
50, 32, 33, 41, 52, 32, 43, 43), Lower_CI = c(36, 36, 36,
15, 8, 31, 30, 14, 18, 23, 38, 37, 40, 21, 15, 31, 40, 17,
24, 27), Upper_CI = c(39, 40, 57, 39, 27, 47, 55, 40, 58,
56, 41, 41, 60, 44, 51, 51, 64, 47, 63, 60), `(-)` = c(2,
2, 10, 12, 10, 8, 13, 13, 20, 17, 1, 2, 10, 11, 18, 10, 12,
15, 19, 16), `(+)` = c(1, 2, 11, 12, 9, 8, 12, 13, 20, 16,
2, 2, 10, 12, 18, 10, 12, 15, 20, 17), CV = c(2, 2, 11, 12,
28, 11, 15, 6, 18, 13, 2, 2, 10, 19, 5, 13, 12, 23, 14, 20
), Sample_size = c(30, 15, 140, 40, 69, 75, 56, 256, 45,
536, 4852, 785, 36, 48, 315, 120, 83, 15, 123, 45), Period = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
You could do something like this:
library(ggplot2)
#create variable for color/fill
df$col <- df$Period
df$col[df$Sample_size < 30 | df$CV > 20] = 3
df$col <- as.factor(df$col)
#plot
ggplot(data = df, aes(x = Group, y = Perc, group = Period,
color = col, fill = col)) +
geom_bar(stat = "identity", position = position_dodge()) +
geom_errorbar(aes(ymin = Lower_CI, ymax = Upper_CI),
position = position_dodge())+
scale_color_manual(values = c("blue", "orange", "grey"),
breaks = c("1", "2"),) +
scale_fill_manual(values = c("blue", "orange", "grey"),
breaks = c("1", "2")) +
coord_flip()+
theme_bw() + theme(legend.title = element_blank())
Related
I have to do a ggplot barplot with errorbars, Tukey sig. letters for plants grown with different fertilizer concentraitions.
The data should be grouped after the dif. concentrations and the sig. letters should be added automaticaly.
I have already a code for the same problem but for Boxplot - which is working nicely. I tried several tutorials with barplots but I always get the problem; stat_count() can only have an x or y aesthetic.
So I thought, is it possible to get my boxplot code to a barplot code? I tried but I couldnt do it :) And if not - how do I automatically add tukeyHSD Test result sig. letters to a ggplot barplot?
This is my Code for the boxplot with the tukey letters:
value_max = Dünger, group_by(Duenger.g), summarize(max_value = max(Höhe.cm))
hsd=HSD.test(aov(Höhe.cm~Duenger.g, data=Dünger),
trt = "Duenger.g", group = T) sig.letters <- hsd$groups[order(row.names(hsd$groups)), ]
J <- ggplot(Dünger, aes(x = Duenger.g, y = Höhe.cm))+ geom_boxplot(aes(fill= Duenger.g))+ scale_fill_discrete(labels=c("0.5g", '1g', "2g", "3g", "4g"))+ geom_text(data = value_max, aes(x=Duenger.g, y = 0.1 + max_value, label = sig.letters$groups), vjust=0)+ stat_boxplot(geom = 'errorbar', width = 0.1)+ ggtitle("Auswirkung von Dünger auf die Höhe von Pflanzen") + xlab("Dünger in g") + ylab("Höhe in cm"); J
This is how it looks:
boxplot with tukey
Data from dput:
structure(list(Duenger.g = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4), plant = c(1, 2, 3, 4, 5, 7, 10, 11, 12, 13, 14, 18, 19,
21, 23, 24, 25, 26, 27, 29, 30, 31, 33, 34, 35, 37, 38, 39, 40,
41, 42, 43, 44, 48, 49, 50, 53, 54, 55, 56, 57, 58, 61, 62, 64,
65, 66, 67, 68, 69, 70, 71, 72, 73, 75, 79, 80, 81, 83, 85, 86,
88, 89, 91, 93, 99, 100, 102, 103, 104, 105, 106, 107, 108, 110,
111, 112, 113, 114, 115, 116, 117, 118, 120, 122, 123, 125, 126,
127, 128, 130, 131, 132, 134, 136, 138, 139, 140, 141, 143, 144,
145, 146, 147, 149), height.cm = c(5.7, 2.8, 5.5, 8, 3.5, 2.5,
4, 6, 10, 4.5, 7, 8.3, 11, 7, 8, 2.5, 7.4, 3, 14.5, 7, 12, 7.5,
30.5, 27, 6.5, 19, 10.4, 12.7, 27.3, 11, 11, 10.5, 10.5, 13,
53, 12.5, 12, 6, 12, 35, 8, 16, 56, 63, 69, 62, 98, 65, 77, 32,
85, 75, 33.7, 75, 55, 38.8, 39, 46, 35, 59, 44, 31.5, 49, 34,
52, 37, 43, 38, 28, 14, 28, 19, 20, 23, 17.5, 32, 16, 17, 24.7,
34, 50, 12, 14, 21, 33, 39.3, 41, 29, 35, 48, 40, 65, 35, 10,
26, 34, 41, 32, 38, 23.5, 22.2, 20.5, 29, 34, 45)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -105L))
Thank you
mirai
A bar chart and a boxplot are two different things. By default geom_boxplot computes the boxplot stats by default (stat="boxplot"). In contrast when you use geom_bar it will by default count the number of observations (stat="count") which are then mapped on y. That's the reason why you get an error. Hence, simply replacing geom_boxplot by geom_bar will not give your your desired result. Instead you could use e.g. stat_summary to create your bar chart with errorbars. Additionally I created a summary dataset to add the labels on the top of the error bars.
library(ggplot2)
library(dplyr)
library(agricolae)
Dünger <- Dünger |>
rename("Höhe.cm" = height.cm) |>
mutate(Duenger.g = factor(Duenger.g))
hsd <- HSD.test(aov(Höhe.cm ~ Duenger.g, data = Dünger), trt = "Duenger.g", group = T)
sig.letters <- hsd$groups %>% mutate(Duenger.g = row.names(.))
duenger_sum <- Dünger |>
group_by(Duenger.g) |>
summarize(mean_se(Höhe.cm)) |>
left_join(sig.letters, by = "Duenger.g")
ggplot(Dünger, aes(x = Duenger.g, y = Höhe.cm, fill = Duenger.g)) +
stat_summary(geom = "bar", fun = "mean") +
stat_summary(geom = "errorbar", width = .1) +
scale_fill_discrete(labels = c("0.5g", "1g", "2g", "3g", "4g")) +
geom_text(data = duenger_sum, aes(y = ymax, label = groups), vjust = 0, nudge_y = 1) +
labs(
title = "Auswirkung von Dünger auf die Höhe von Pflanzen",
x = "Dünger in g", y = "Höhe in cm"
)
#> No summary function supplied, defaulting to `mean_se()`
But as the summary dataset now already contains the mean and the values for the error bars a second option would be to do:
ggplot(duenger_sum, aes(x = Duenger.g, y = y, fill = Duenger.g)) +
geom_col() +
geom_errorbar(aes(ymin = ymin, ymax = ymax), width = .1) +
scale_fill_discrete(labels = c("0.5g", "1g", "2g", "3g", "4g")) +
geom_text(aes(y = ymax, label = groups), vjust = 0, nudge_y = 1) +
labs(
title = "Auswirkung von Dünger auf die Höhe von Pflanzen",
x = "Dünger in g", y = "Höhe in cm"
)
In order to overlay a movement network on a spatial map, I'm trying to define my own custom layout for plotting geocoordinates as node positions, using the packages igraph, ggraph and ggplot2, among others. The below code used to work but not anymore. Perhaps because i updated packages but i cannot remember. I cannot figure out how to solve the error message that i'm receiving below. Any help with solving is greatly appreciated.
The relevant code to create a custom layout are below.
igraph graph 'g':
g<-structure(list(32, FALSE, c(1, 4, 5, 6, 7, 8, 9, 10, 11, 13,
14, 15, 18, 19, 20, 22, 25, 27, 28, 31, 4, 6, 7, 9, 18, 22, 6,
7, 9, 13, 14, 18, 27, 9, 18, 7, 8, 9, 18, 22, 27, 9, 18, 10,
17, 18, 22, 27, 18, 18, 22, 18, 22, 27, 27), c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
4, 4, 4, 4, 4, 4, 4, 5, 5, 6, 7, 7, 7, 7, 7, 8, 8, 9, 9, 9, 9,
9, 10, 13, 13, 14, 18, 18, 22), c(0, 1, 20, 2, 3, 21, 26, 4,
22, 27, 35, 5, 36, 6, 23, 28, 33, 37, 41, 7, 43, 8, 9, 29, 10,
30, 11, 44, 12, 24, 31, 34, 38, 42, 45, 48, 49, 51, 13, 14, 15,
25, 39, 46, 50, 52, 16, 17, 32, 40, 47, 53, 54, 18, 19), c(0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54), c(0, 0, 1, 1, 1, 3, 4, 7, 11, 13, 19, 21, 22,
22, 24, 26, 27, 27, 28, 38, 39, 40, 40, 46, 46, 46, 47, 47, 53,
54, 54, 54, 55), c(0, 20, 26, 26, 26, 33, 35, 36, 41, 43, 48,
49, 49, 49, 51, 52, 52, 52, 52, 54, 54, 54, 54, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55), list(c(1, 0, 1), structure(list(), .Names = character(0)),
list(name = c("1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
"30", "31", "32"), long = c(-79.260866474359, -80.0214733333333,
-76.7089527272727, -74.780105, -80.2763261538462, -80.46082,
-80.030754125, -80.121487, -82.1419016578947, -81.41021461,
-85.6918532152941, -81.2031545454546, -81.187526, -80.11719,
-80.5063443067816, -82.5245573333333, -78.8618875, -80.883413,
-79.7296606, -76.9488, -77.8201766666667, -76.5490625, -81.5434341458974,
-93.59755, -88.035675, -83.2922209874074, -84.5110066666667,
-79.1122080625, -80.173172, -77.3383016666667, -78.2188216666667,
-77.7654788888889), lat = c(25.7205797179487, 26.85296, 25.53793,
38.33654, 27.1842307692308, 24.948536, 26.99901625, 27.171273,
28.2081715526316, 24.6188354115741, 29.4212288229412, 31.0068890909091,
29.643964, 26.6099285714286, 28.4826687301149, 27.100245,
26.63643, 31.3834996666667, 32.6213106, 34.2073, 24.8982166666667,
24.3104225, 24.6719581623077, 27.90902, 30.0528425, 27.764016172963,
29.706515, 26.97782228125, 25.5103912857143, 34.55239, 26.6567083333333,
26.9487844444444)), list(weight = c(426, 58, 3, 253, 493,
9, 392, 7, 4, 42, 5, 2, 40, 1, 2, 74, 2, 106, 17, 1, 2, 114,
62, 9, 1, 1, 1, 5, 3, 12, 1, 9, 1, 2, 1, 1, 1, 15, 6, 2,
17, 4, 1, 1, 1, 21, 4, 4, 1, 1, 1, 1, 2, 1, 2))), <environment>), class = "igraph")
Node positions:
nodes_pos<-structure(list(x = c(-79.260866474359, -80.0214733333333, -76.7089527272727,
-74.780105, -80.2763261538462, -80.46082, -80.030754125, -80.121487,
-82.1419016578947, -81.41021461, -85.6918532152941, -81.2031545454546,
-81.187526, -80.11719, -80.5063443067816, -82.5245573333333,
-78.8618875, -80.883413, -79.7296606, -76.9488, -77.8201766666667,
-76.5490625, -81.5434341458974, -93.59755, -88.035675, -83.2922209874074,
-84.5110066666667, -79.1122080625, -80.173172, -77.3383016666667,
-78.2188216666667, -77.7654788888889), y = c(25.7205797179487,
26.85296, 25.53793, 38.33654, 27.1842307692308, 24.948536, 26.99901625,
27.171273, 28.2081715526316, 24.6188354115741, 29.4212288229412,
31.0068890909091, 29.643964, 26.6099285714286, 28.4826687301149,
27.100245, 26.63643, 31.3834996666667, 32.6213106, 34.2073, 24.8982166666667,
24.3104225, 24.6719581623077, 27.90902, 30.0528425, 27.764016172963,
29.706515, 26.97782228125, 25.5103912857143, 34.55239, 26.6567083333333,
26.9487844444444)), row.names = 1:32, class = "data.frame")
manual layout:
lay <- create_layout(graph = g, layout = "manual",
node.positions = nodes_pos)
Error message
Error in layout_fun(graph, circular = circular, ...) :
unused argument (node.positions = nodes_pos)
Package versions:
ggraph 2.0.2
igraph 1.2.4.2
R 3.6.2
try:
g_new <- upgrade_graph(g)
lay <- create_layout(g_new, 'manual',
x = long, y = lat)
ggraph(lay) +
geom_node_point(color = "red") +
geom_polygon(data = map_data("state"), aes(x=long, y=lat, group=group),
color="black", fill="lightblue" )
since yesterday I am reading answers and websites in order to combine and align in one plot an histogram and a boxplot generated using ggplot2 package.
This question differs from others because the boxplot chart needs to be reduced in height and aligned to the left outer margin of the histogram.
Considering the following dataset:
my_df <- structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
92, 93, 94, 95, 96, 97, 98, 99, 100), value= c(18, 9, 3,
4, 3, 13, 12, 5, 8, 37, 64, 107, 11, 11, 8, 18, 5, 13, 13, 14,
11, 11, 9, 14, 11, 14, 12, 10, 11, 10, 5, 3, 8, 11, 12, 11, 7,
6, 6, 4, 11, 8, 14, 13, 14, 15, 10, 2, 4, 4, 8, 15, 21, 9, 5,
7, 11, 6, 11, 2, 6, 16, 5, 11, 21, 33, 12, 10, 13, 33, 35, 7,
7, 9, 2, 21, 32, 19, 9, 8, 3, 26, 37, 5, 6, 10, 18, 5, 70, 48,
30, 10, 15, 18, 7, 4, 19, 10, 4, 32)), row.names = c(NA, 100L
), class = "data.frame", .Names = c("id", "value"))
I generated the boxplot:
require(dplyr)
require(ggplot2)
my_df %>% select(value) %>%
ggplot(aes(x="", y = value)) +
geom_boxplot(fill = "lightblue", color = "black") +
coord_flip() +
theme_classic() +
xlab("") +
theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank())
and I generated the histogram
my_df %>% select(id, value) %>%
ggplot() +
geom_histogram(aes(x = value, y = (..count..)/sum(..count..)),
position = "identity", binwidth = 1,
fill = "lightblue", color = "black") +
ylab("Relative Frequency") +
theme_classic()
The result I am looking to obtain is a single plot like:
Note that the boxplot must be reduced in height and the ticks must be exactly aligned in order to give a different perspective of the same visual.
You can use either egg, cowplot or patchwork packages to combine those two plots. See also this answer for more complex examples.
library(dplyr)
library(ggplot2)
plt1 <- my_df %>% select(value) %>%
ggplot(aes(x="", y = value)) +
geom_boxplot(fill = "lightblue", color = "black") +
coord_flip() +
theme_classic() +
xlab("") +
theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank())
plt2 <- my_df %>% select(id, value) %>%
ggplot() +
geom_histogram(aes(x = value, y = (..count..)/sum(..count..)),
position = "identity", binwidth = 1,
fill = "lightblue", color = "black") +
ylab("Relative Frequency") +
theme_classic()
egg
# install.packages("egg", dependencies = TRUE)
egg::ggarrange(plt2, plt1, heights = 2:1)
cowplot
# install.packages("cowplot", dependencies = TRUE)
cowplot::plot_grid(plt2, plt1,
ncol = 1, rel_heights = c(2, 1),
align = 'v', axis = 'lr')
patchwork
# install.packages("devtools", dependencies = TRUE)
# devtools::install_github("thomasp85/patchwork")
library(patchwork)
plt2 + plt1 + plot_layout(nrow = 2, heights = c(2, 1))
Consider the following data.frame
RANK_GROUP <- as.factor(c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))
CHANNEL_CATEGORY <- as.factor(c(1, 2, 10, 15, 17, 19, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 43, 44, 1, 2, 10, 15, 17, 19, 20, 22, 23, 24, 25, 26, 27, 28, 29, 43))
CATEGORY_COUNT <- c(105, 23, 417, 10, 58, 6, 535, 211, 215, 465, 28, 273, 70, 47, 7,1,21,3,69, 14, 493, 3, 44, 3, 516, 162, 253, 516, 24, 228, 64, 59, 2, 45)
data <- data.frame(RANK_GROUP, CHANNEL_CATEGORY,CATEGORY_COUNT)
I want to make a Facet-Plot with a barplot for each distribution:
ggplot(data = data) +
aes(x=CHANNEL_CATEGORY, y = CATEGORY_COUNT) +
geom_bar(stat="identity", position ="dodge", colour="black") +
facet_grid(. ~ RANK_GROUP)
How can I order the plots according to their y-value withing each facet-plot?
took the help of cookbook,
library(dplyr)
pd <- data %>%
group_by(RANK_GROUP) %>%
top_n(nrow(data), abs(CATEGORY_COUNT)) %>%
ungroup() %>%
arrange(RANK_GROUP, CATEGORY_COUNT) %>%
mutate(order = row_number())
pd$order <- as.factor(pd$order)
ggplot(data = pd) +
aes(x=order, y = CATEGORY_COUNT) +
geom_bar(stat="identity", position ="dodge", colour="black") +
facet_grid(. ~ RANK_GROUP)+
scale_x_discrete(labels = CHANNEL_CATEGORY , breaks = order)+
theme(axis.text.x = element_text(angle = 60, hjust = .5, size = 8)) +
labs(x="Channel")
I have plotted a scatter plot with the point size scaled by frequency:
g<-ggplot(d, aes(x=Treatment, y= Seam.Cell.Number, size=Frequency))+geom_point(aes(colour=Strain))+ scale_size_continuous(range = c(3, 10), breaks=c(0,1, 2, 3, 4, 5,6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50))+guides(size=FALSE)
Now I am trying to plot means with standard error bars on top. I have the mean and standard error already calculated in columns in my csv file. So so far I have attempted:
g+geom_point(aes(x=Treatment,y=Mean))+geom_errorbar(aes(ymin=Mean-Standard.Error, ymax=Mean+Standard.Error, width=.4))+theme(axis.text.x = element_blank())+theme(legend.key = element_rect(colour = "black"))
And:
g+layer(data=d, mapping=aes(x=Treatment,y=Mean), geom="point")+geom_errorbar(aes(ymin=Mean-Standard.Error, ymax=Mean+Standard.Error), width=.4)+ylab("Seam Cell Number")
But they both give me very fat error bars/data points. It seems they are being affected by my size scaling in object g. I have tried to modify the size and width of the error bars, and I have tried to modify the size of the data points, both in these last bits of code, but to no avail. Is there a way to 'cancel' the size command for this layer?
If you reverse the order of your ggplot, you may be able to avoid the size distortion on the error bars.
Not having reproducible data, I made some up.
df <- data.frame(Treatment = (1:100), Seam.Cell.Number = 3:102, Frequency = 5:104,
Strain = rep(c("A", "B", "C", "D"), 25))
std <- function(x) sd(x)/sqrt(length(x))
Mean <- mean(df$Treatment)
df$Standard.Error <- std(df$Treatment)
g <- ggplot(df, aes(x = Treatment, y = Seam.Cell.Number)) +
geom_point(aes(x=Treatment, y=Mean)) +
geom_errorbar(aes(ymin=Mean-df$Standard.Error, ymax=Mean+df$Standard.Error, width=.4))+
theme(axis.text.x = element_blank())+
theme(legend.key = element_rect(colour = "black"))
g + geom_point(aes(colour=Strain)) +
scale_size_continuous(range = c(3, 10), breaks=c(0,1, 2, 3, 4, 5,6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 41, 42,
43, 44, 45, 46, 47, 48, 49, 50)) +
guides(size=FALSE)