scaling y-axes while using facet_wrap in ggplot - r

I am trying to combine different y-axes while using facet_wrap, but I want all the y-axes to have exactly 3 breaks based in the min and max of that scale, so 0, mid-point, and maximum. It works for some of the plots on the facet grid but not all of them.
Here is the current code:
# data table, in long format
dt = structure(list(Grp = c(rep("GroupA",12),rep("GroupB",12),rep("GroupC",12),rep("GroupD",12)), Type = c(rep(c(rep("Type1",5),rep("Type2",7)),4)), XVal = c(2L, 3L, 4L, 5L, 6L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 2L,
3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), YVal = c(0.2417, 0.2156, 0.264,
0.2805, 0.2414, 0.2882, 0.0825, 0.0561, 0.1443, 0.1074, 0.0252,
1e-04, 0.0186, 0.0157, 0.0473, 0.13, 0.1205, 0.0689, 0.1506,
0.2945, 0.3098, 0.1474, 0.3408, 0.1327, 0.0102, 0.0033, 0.0021,
2e-04, 0, 0.0124, 0.0053, 0.0039, 0.0014, 4e-04, 0, 0, 0.0574,
0.1003, 0.0687, 0.0976, 0.1067, 0.1161, 0.0964, 0.0517, 0.0658,
0.0654, 0.0241, 0.0021)), row.names = c(NA,-48L), class = "data.frame")
# first created a function to define three breaks, and round to 2 decimal points
my_breaks <- function(y) {round(seq(0, max(y),length.out = 3),2)}
# use that function in the 'scale_y_continuous' while specifying that the y scale is "free" in facet_wrap
ggplot(dt,aes(x=XVal,y=YVal)) + geom_line(aes(color=Type)) +
facet_wrap(~Grp,scales = "free_y", ncol = 2) +
scale_y_continuous(breaks = my_breaks)
The breaks are what I want for Groups A & D but not B & C. Any help would be greatly appreciated.

It looks like it has something to do with how the rounding is affecting the results of your my_breaks function. If you remove the round so your function looks like
my_breaks <- function(y) {seq(0, max(y),length.out = 3)}
Each facet has exactly three evenly spaced breaks. Now we can format the numbers in the plot:
ggplot(dt,aes(x=XVal,y=YVal)) + geom_line(aes(color=Type)) +
facet_wrap(~Grp,scales = "free_y", ncol = 2) +
scale_y_continuous(breaks = my_breaks,
labels = function(x){round(x,2)})
Notice, however that in Group C, the labels end up not making total sense, since both values for the breaks (0.013 and 0.006) round to 0.01.

Related

How to prevent R from alphabetically ranking data in ggplot and specify the order in which data is plotted (Data + Code + Graphs provided)?

I'm trying to fix an issue with my GGBalloonPlot graph with regards to how R processes the axis labels.
By default R plots the data using the labels ranked in reverse alphabetical order but to reveal the pattern of the data, the data need to be plotted in a specific order. The only way I've been able to do trick the software is by manually adding a prefix to each label in my .csv table so that R would rank them properly in my output. This is time consuming since I need to manually order the data first before adding the prefix and then plotting.
I would like to input a character vector (or something like that) which would essentially specify the order in which I want to have the data plotted which would reveal the pattern without the need for a prefix in the label name.
I have made some attempts with "scale_y_discrete" without success. I would also like to do the same thing for the X axis since I've had to use the same "trick" to display the columns in the proper non-alphabetical order which offsets the position of the labels. Any idea on how to get GGplot to display my values as seen in the graph without having to "trick" the software since this is quite time consuming ?
Data + Code
#Assign data to "Stack_Overflow_DummyData"
Stack_Overflow_DummyData <- structure(list(Species = structure(c(8L, 3L, 1L, 5L, 6L, 2L,
7L, 4L, 8L, 3L, 1L, 5L, 6L, 2L, 7L, 4L, 8L, 3L, 1L, 5L, 6L, 2L,
7L, 4L, 8L, 3L, 1L, 5L, 6L, 2L, 7L, 4L), .Label = c("Ani", "Cal",
"Can", "Cau", "Fis", "Ort", "Sem", "Zan"), class = "factor"),
Species_prefix = structure(c(8L, 7L, 6L, 5L, 4L, 3L, 2L,
1L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 8L, 7L, 6L, 5L, 4L, 3L,
2L, 1L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L), .Label = c("ac.Cau",
"ad.Sem", "af.Cal", "ag.Ort", "as.Fis", "at.Ani", "be.Can",
"bf.Zan"), class = "factor"), Dist = structure(c(2L, 3L,
5L, 2L, 1L, 1L, 4L, 5L, 2L, 3L, 5L, 2L, 1L, 1L, 4L, 5L, 2L,
3L, 5L, 2L, 1L, 1L, 4L, 5L, 2L, 3L, 5L, 2L, 1L, 1L, 4L, 5L
), .Label = c("End", "Ind", "Pan", "Per", "Wid"), class = "factor"),
Region = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Cen", "Col",
"Far", "Nor"), class = "factor"), Region_prefix = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L), .Label = c("a.Far", "b.Nor", "c.Cen", "d.Col"), class = "factor"),
Frequency = c(75, 50, 25, 50, 0, 0, 0, 0, 11.1, 22.2, 55.6,
55.6, 11.1, 0, 5.6, 0, 0, 2.7, 36.9, 27.9, 65.8, 54.1, 37.8,
28.8, 0, 0, 0, 3.1, 34.4, 21.9, 78.1, 81.3)), class = "data.frame", row.names = c(NA,
-32L))
# Plot Data With Prefix Trick
library(ggplot2)
library(ggpubr)
# make color base on Dist, size and alpha dependent on Frequency
ggballoonplot(Stack_Overflow_DummyData, x = "Region_prefix", y = "Species_prefix",
size = "Frequency", size.range = c(1, 9), fill = "Dist") +
theme_set(theme_gray() +
theme(legend.key=element_blank())) +
# Sets Grey Theme and removes grey background from legend panel
theme(axis.title = element_blank()) +
# Removes X axis title (Region)
geom_text(aes(label=Frequency), alpha=1.0, size=3, nudge_x = 0.4)
# Add Frequency Values Next to the circles
# Plot Data Without Prefix Trick
library(ggplot2)
library(ggpubr)
# make color base on Dist, size and alpha dependent on Frequency
ggballoonplot(Stack_Overflow_DummyData, x = "Region", y = "Species",
size = "Frequency", size.range = c(1, 9), fill = "Dist") +
theme_set(theme_gray() +
theme(legend.key=element_blank())) +
# Sets Grey Theme and removes grey background from legend panel
theme(axis.title = element_blank()) +
# Removes X axis title (Region)
geom_text(aes(label=Frequency), alpha=1.0, size=3, nudge_x = 0.4)
# Add Frequency Values Next to the circles
Here below are the graphs
Good Graph.
Using the label prefix trick with the visible pattern in the data:
Wrong Graph (R default).
Without the prefix trick when GGplot automatically orders the data/labels and the graph makes no sense:
To sum up, I would like the Good graph output without having to have to previously add a prefix in my labels.
Many Thanks in advance for your help.
For the axis labels I would define a previous function to override the breaks:
shlab <- function(lbl_brk){
sub("^[a-z]+\\.","",lbl_brk) # removes the starts of strings as a. or ab.
}
Then, to change the labels you just have to use scale_x,y_discrete with labels = shlab (if you look at the help of scale_x_discrete you will see that one of the options for labels is A function that takes the breaks as input and returns labels as output).
For the colours would be enough to change them (values) in scale_fill_manual and for the sizes, using guides so:
library(ggplot2)
library(ggpubr)
shlab <- function(lbl_brk){
sub("^[a-z]+\\.","",lbl_brk)
}
ggballoonplot(Stack_Overflow_DummyData, x = "Region_prefix", y = "Species_prefix", size = "Frequency", size.range = c(1, 9), fill = "Dist") +
scale_x_discrete(labels = shlab) +
scale_y_discrete(labels = shlab) +
scale_fill_manual(values = c("green", "blue", "red", "black", "white")) +
guides(fill = guide_legend(override.aes = list(size=8))) +
theme_set(theme_gray() + theme(legend.key=element_blank())) + # Sets Grey Theme and removes grey background from legend panel
theme(axis.title = element_blank()) + # Removes X axis title (Region)
geom_text(aes(label=Frequency), alpha=1.0, size=3, nudge_x = 0.4) # Add Frequency Values Next to the circles
UPDATE:
With the new dataset and vector labels:
library(ggplot2)
library(ggpubr)
# make color base on Dist, size and alpha dependent on Frequency
ggballoonplot(Stack_Overflow_DummyData, x = "Region", y = "Species",
size = "Frequency", size.range = c(1, 9), fill = "Dist") +
scale_y_discrete(limits = c("Cau", "Sem", "Cal", "Ort", "Fis", "Ani", "Can", "Zan")) +
scale_x_discrete(limits = c("Far", "Nor", "Cen", "Col")) +
theme_set(theme_gray() +
theme(legend.key=element_blank())) +
# Sets Grey Theme and removes grey background from legend panel
theme(axis.title = element_blank()) +
# Removes X axis title (Region)
geom_text(aes(label=Frequency), alpha=1.0, size=3, nudge_x = 0.4)

Regression of multiple dose-response curves using the drc package in R

I am trying to fit regressions (4 or 5 PL) through my experimental data. I have several compounds inhibiting my enzyme of interest. Each has it's own range between 0-100% enzyme activity. All the data is in one dataframe and dinstinguished by one column specifying my compound ('toxin'). Therefore I want to have a regression for each of the toxins/compounds individually. I tried the following code
drc <- drm(avg ~ conc, data = testdata, toxin, fct = LL.5())
which gives the two following errors:
Error in optim(startVec, opfct, hessian = TRUE, method = optMethod,
control = list(maxit = maxIt, : non-finite finite-difference value
[24] Error in drmOpt(opfct, opdfct1, startVecSc, optMethod,
constrained, warnVal, : Convergence failed
After reading some posts on SO, this error was often solved by not using a log scale for the concentration ('conc'). In my case, the data is not log-transformed, therefore I do not really know how to proceed as I do not really understand what the error-message is telling me.
I tried the same command with only a subset of the data (only one of the toxins) and that works.
Here is the data:
testdata <- structure(list(toxin = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("toxin1",
"toxin2", "toxin3", "toxin4", "toxin5", "toxin6", "NC", "PC",
"toxin7"), class = "factor"), conc = c(80, 230, 690, 2060,
6170, 18520, 55560, 116700, 5e+05, 1500000, 10, 30, 100, 290,
860, 2600, 7700, 23300, 70000, 210000, 0.25, 0.76, 2.29, 6.69,
29.57, 61.73, 185.19, 555.56, 1666.67, 5000, 0.1, 0.3, 0.91,
2.74, 8.23, 24.69, 74.07, 222.22, 666.67, 2000, 0.19, 0.39, 0.78,
1.56, 3.125, 6.25, 12.5, 25, 50, 100, 0.05, 0.14, 0.41, 1.23,
3.7, 11.11, 33.33, 100, 300, 900, 0.25, 0.76, 2.29, 6.69, 20.57,
61.73, 185.19, 555.56, 1666.67, 5000), avg = c(93.7392909656605,
109.438977761257, 102.50389863782, 97.8565582988098, 98.7749196390328,
94.6820096545283, 88.3878644123183, 74.6531623906189, 59.8033994067719,
33.1521812859023, 84.3458578131283, 80.8432075369312, 80.5041552022783,
74.3806536115552, 65.867746238255, 46.7093609589345, 25.2625895634089,
16.5991924099889, 9.8338847737454, 9.1267136985971, 96.7637675923354,
100.217322048861, 106.911067427548, 105.869274152439, 104.26295691452,
99.924974639669, 105.178112603458, 100.834869287621, 97.0640881891228,
100.517438616909, 102.664029650058, 104.079019894009, 106.005108031173,
101.539083701953, 98.0496674854621, 67.7840816081928, 39.3101865930841,
38.410593148271, 8.98193991681226, 7.22314661576326, 84.0614720922454,
82.7675961061481, 65.2085894181738, 37.3278677636159, 24.9075938602538,
14.3617392491638, 10.7917687047216, 8.37929257644196, 8.42895771412019,
12.9194757988616, 76.5674185459266, 65.8625860764468, 47.7169920989096,
29.6780563387259, 7.69651805994566, 4.34554390880982, 4.33821927277971,
0.39797595095055, 2.38671848257005, 5.89474149920234, 107.319075979956,
110.227548845268, 116.828640966343, 107.913632096559, 110.071386130938,
106.575197414688, 105.043139402911, 98.236919454246, 104.052659508375,
84.6763301224036), sd = c(7.49544951952132, 14.9170973650272,
1.03754566304896, 3.87773637652399, 9.17174603323541, 2.0257944547102,
0.874956239047901, 3.35155947287539, 1.91936941393018, 2.02594096726786,
1.60035835782164, 1.25579403370456, 3.52866856497447, 4.04640886982452,
7.37920326517342, 6.40246869316039, 4.77482079353957, 4.68322190067079,
1.74780492483205, 0.738821067897037, 5.42050977224004, 12.2951096302121,
9.08089564089922, 7.46281702965045, 9.52060311645085, 6.66339041948764,
9.04568668161887, 10.9590666295114, 6.25902541715453, 4.96928340386536,
10.8885949633507, 15.9830841613276, 7.11298501037955, 8.54768106201583,
12.7115587453605, 5.72457692384765, 4.62110397186864, 50.9817341717873,
2.96030364454981, 2.83464116977327, 10.7124422767561, 10.3544552730142,
9.05103847553877, 13.233995551835, 4.26528894064237, 2.18416799462023,
1.17346307923401, 5.46453008680512, 3.09705214055433, 10.1345046611914,
2.11845922287944, 3.11915150865922, 6.31893385595251, 14.1295842962481,
1.33224797602539, 2.11901484197009, 5.05792906176149, 2.08503325893712,
3.05243406958019, 8.68923158027763, 8.49552648053034, 7.45485150355005,
8.70510335269844, 7.13998242209083, 6.32588028411456, 4.75860842345735,
4.09767898578108, 7.04991004776136, 9.37260366463128, 7.20137530818876
)), .Names = c("toxin", "conc", "avg", "sd"), row.names = c(NA,
-70L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = list(
toxin), drop = TRUE, indices = list(0:9, 10:19, 20:29, 30:39,
40:49, 50:59, 60:69), group_sizes = c(10L, 10L, 10L, 10L,
10L, 10L, 10L), biggest_group_size = 10L, labels = structure(list(
toxin = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 9L), .Label = c("toxin1",
"toxin2", "toxin3", "toxin4", "toxin5", "toxin6", "NC",
"PC", "toxin7"), class = "factor")), row.names = c(NA,
-7L), class = "data.frame", vars = list(toxin), drop = TRUE, .Names = "toxin"))
The error message is telling you that the function could not find a solution for at least one of your data subsets. In your case, toxin3 is the offending dataset. If I run the following code which omits toxin3, I get a converged result and a nice nice plot showing the results.
drc <- drm(avg ~ conc, data = testdata, curveid=toxin, subset=toxin %in% c("toxin1","toxin2","toxin4","toxin5","toxin6","toxin7"), fct = LL.5())
plot(drc)
The reason toxin 3 fails is that the data describe a flat line that could be fit by any number of sets of LL.5() parameters. One way to get around these this would be to fit each curve separately in a loop (or apply function) and use try/catch to handle any datasets that throw a convergence error.

New error when producing boxplot

So I had this script working yesterday on a different data set, an it actually worked once on this data set, but when I tried to combine it with another figure using plot_grid, I got this error:
Error:
T_SHOW_BACKTRACE environmental variable.
Error in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
polygon edge not found
Now when I try to construct the boxplot itself, I get the same error...
Here is my data:
dput(SUICMass)
structure(list(ChillTime = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("2", "4", "6", "24",
"27", "29", "31"), class = "factor"), Mass = c(1.2687, 1.5417,
1.6898, 1.7655, 2.413, 2.0333, 2.0824, 1.2676, 1.4916, 2.1585,
2.2453, 1.3624, 1.2951, 2.4209, 2.0804, 1.9227, 1.9032, 2.1063,
1.7601, 1.9905, 1.9837, 1.6312, 1.8567, 1.4433, 1.9369, 2.1029,
2.0265, 1.3212, 1.2971, 1.5823, 1.4759, 1.2745, 0.714, 1.5693,
1.7906, 1.8607, 1.8851, 1.9192, 1.6307, 1.4269, 1.7011, 0.8249,
1.7198, 1.3939, 1.394, 2.1527, 1.288, 1.4724, 1.5264, 1.6562,
1.5796, 1.4982, 1.2794, 1.6021, 0.6345, 2.4041, 2.0246, 1.8398,
1.349, 2.0156, 1.1563, 2.0462)), .Names = c("ChillTime", "Mass"
), row.names = c(NA, -62L), class = "data.frame")
Here is my code:
library(ggplot2)
library(multcompView)
library(plyr)
library(gridExtra)
library(cowplot)
## Box plot for Susans WMA population
SUICMass <- read.csv('SUICMass_Test_June_28_2017.csv', header = TRUE)
SUICMass$ChillTime <- factor(SUICMass$ChillTime, levels=c("2", "4", "6", "24", "27", "29", "31"))
generate_label_df <- function(SUICMassTUKEY, variable){
# Extract labels and factor levels from Tukey post-hoc
Tukey.levels <- SUICMassTUKEY[[variable]][,4]
Tukey.labels <- data.frame(multcompLetters(Tukey.levels)['Letters'])
#I need to put the labels in the same order as in the boxplot :
Tukey.labels$treatment=rownames(Tukey.labels)
Tukey.labels=Tukey.labels[order(Tukey.labels$treatment) , ]
return(Tukey.labels)
}
SUICMassmodel=lm(SUICMass$Mass~SUICMass$ChillTime )
SUICMassANOVA=aov(SUICMassmodel)
# Tukey test to study each pair of treatment :
SUICMassTUKEY <- TukeyHSD(x=SUICMassANOVA, 'SUICMass$ChillTime', conf.level=0.95)
labels<-generate_label_df(SUICMassTUKEY , "SUICMass$ChillTime")#generate labels using function
names(labels)<-c('Letters','ChillTime')#rename columns for merging
SUICMassyvalue<-aggregate(.~ChillTime, data=SUICMass, max)# obtain letter position for y axis using means
SUICMassfinal<-merge(labels,SUICMassyvalue) #merge dataframes
SUICMassPlot <- ggplot(SUICMass, aes(x = ChillTime, y = Mass)) +
stat_boxplot(geom ='errorbar', width=.2) +
geom_blank() +
theme_bw() +
theme(panel.border = element_rect(fill=NA, colour = "black", size=0.75)) +
theme(axis.text.x = element_text(face="bold")) +
theme(axis.text.y = element_text(face="bold")) +
labs(x = 'Time (weeks)', y = 'Mass (g)') +
ggtitle(expression(atop(bold("Fresh Mass"), atop(italic("(Sarah's - UIC Colony)"))))) +
theme(plot.title = element_text(hjust = 0.5, vjust = -0.6, face='bold')) +
geom_boxplot(fill = 'dodgerblue1', stat = "boxplot") +
geom_text(data = SUICMassfinal, aes(x = ChillTime, y = Mass, label = Letters),vjust=-2,hjust=.5) +
scale_y_continuous(limit = c(0, 3.5))
I can't figure out what the issue is here, because sometimes I can get the script to work and other times not.

How to add multiple data series to a scatterplot and how to format numbers to appear in standard form on y axis

My data set:
structure(list(Site = c(2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L), Average.worm.weight..g. = c(0.1934,
0.249, 0.263, 0.262, 0.4186, 0.204, 0.311, 0.481, 0.326, 0.657,
0.347, 0.311, 0.239, 0.4156, 0.31, 0.3136, 0.4033, 0.302, 0.277
), Average.total.immune.cell.count = structure(c(8L, 16L, 11L,
12L, 10L, 1L, 4L, 15L, 4L, 3L, 17L, 13L, 18L, 7L, 5L, 6L, 9L,
14L, 2L), .Label = c("0", "168750", "18650000", "200,000", "21,600,000",
"226666.6", "22683333.33", "2533333.33", "283333.333", "291666.6",
"335833.3", "435800", "474816666.7", "500000", "6450000", "729166.667",
"7433333.3", "9916667"), class = "factor"), Average.eleocyte.number = structure(c(2L,
5L, 14L, 10L, 1L, 1L, 6L, 1L, 6L, 7L, 1L, 9L, 15L, 8L, 12L, 3L,
11L, 13L, 4L), .Label = c("0", "1266666.67", "153333.3", "168740",
"17", "200,000", "2266666.667", "22683333.33", "23116666.67",
"264000", "283333.333", "442", "500000", "7.3", "9916667"), class = "factor")), .Names = c("Site",
"Average.worm.weight..g.", "Average.total.immune.cell.count",
"Average.eleocyte.number"), class = "data.frame", row.names = c(NA,
-19L))
This is my R script so far:
Plotting multiple data series on a graph
y1<-dframe1$"Average.total.immune.cell.count"
y2<-dframe1$"Average.eleocyte.number"
x<-dframe1$"Average.worm.weight..g."
plot.default(y1~x,type="p" )
points(y2~x)
I am trying to add to y series to the same scatterplot and I am struggling to do so, I want to have different symbols for the points so as to tell apart the two different data series. Also I would like the axes to meet on the bottom left hand side and would appreciate being informed as to how I can do that? I would also like the y axis to be in standard form, but do not know how to get R to do that.
Best regards.
K.
So this is an object lesson is getting your data in the correct format to begin with. Your numbers have commas, which R does not like. Hence the numbers get converted to character and imported as factors (which your structure(...) clearly shows. You need to fix that, or better yet get rid of the commas prior to exporting.
Something like this will work
colnames(dframe) <- c("Site","x","y1","y2")
dframe$y1 <- as.numeric(as.character(gsub(",","",dframe$y1,fixed=TRUE)))
dframe$y2 <- as.numeric(as.character(gsub(",","",dframe$y2,fixed=TRUE)))
plot(y1~x,dframe, col="red", pch=20)
points(y2~x,dframe, col="blue", pch=20)
But there are additional problems. One of the numbers (in row 12) is a factor of 10 larger than all the others, so the plot above is not very informative. It's hard to know if this is a data input error, or a genuine outlier in your data.
EDIT: Response to OP's comment
dframe <- dframe[-12,] # remove row 12
dframe <- dframe[order(dframe$x),] # order by increasing x
plot(y1~x,dframe, col="red", pch=20, type="b")
points(y2~x,dframe, col="blue", pch=20, type="b")
legend("topleft",legend=c("y1","y2"),col=c("red","blue"),pch=20)

ggplot barchart with grouped confidence interval

a <- structure(list(
X1 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L),
.Label = c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8"), class = "factor"),
X2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L),
.Label = c("A", "B", "C"), class = "factor"),
value = c(0.03508924, 0.03054929, 0.03820896, 0.18207091, 0.25985142, 0.03909991, 0.03079736,
0.41436334, 0.02957787, 0.03113289, 0.03239794, 0.1691519, 0.16368845, 0.0287741, 0.02443448,
0.33474091, 0.03283068, 0.02668754, 0.03597605, 0.17098721, 0.23048966, 0.0385765, 0.02597068, 0.36917749),
se = c(0.003064016, 0.003189752, 0.003301929, 0.006415592, 0.00825635, 0.003479607,
0.003195332, 0.008754099, 0.005594554, 0.006840959, 0.006098068, 0.012790908, 0.014176414,
0.006249045, 0.005659445, 0.018284739, 0.005051873, 0.004719352, 0.005487301, 0.011454206,
0.01290797, 0.005884275, 0.004738851, 0.014075813)),
.Names = c("X1", "X2", "value", "se"), class = "data.frame", row.names = c(NA, -24L))
I'm plotting the above data (kept in dataset "a"), and I can't get the confidence interals to sit in the middle of the group chart.My attempts until now have only managed to put lines on the side of each bar, not in the middle like in the geom_errorbar helpfile.I've tried to manipulate the dodge parameters but it only made it worse.
The chart needs to stay flipped over and in the code below I used geom_linerange but geom_errorbar would be even better.
Another thing I haven't quite managed to do is to change the scale into whole numbers (without muliplying the original table ).
I've used the code below on a<-a[1:16,] (the first two groups).
When I use the same code on the full table I get even worse results with the confidence intervals.
Would anyone be able to help? Many thanks in advance.
limits <- aes(ymax = value + se, ymin=value - se)
p<-ggplot(data = a, aes(x = X1, y =value))+
geom_bar(aes(fill=X2),position = "dodge") +
scale_x_discrete(name="")+
scale_fill_manual(values=c("grey80","black","red"))+
scale_y_continuous(name="%")+
theme(axis.text.y = element_text(face='bold'),
legend.position ="top",
legend.title=element_blank())+
coord_flip()
p + geom_linerange(limits)
Try this ,
p<-ggplot(data = df, aes(x = X1, y =value,fill= X2))+
geom_bar(position=position_dodge()) +
geom_errorbar(aes(ymax = value + 2* se, ymin=value,colour = X2),position=position_dodge(.9))
p <- p + scale_x_discrete(name="")+
scale_fill_manual(values=c("grey80","black","red"))+
scale_y_continuous(name="%")+
theme(axis.text.y = element_text(face='bold'),
legend.position ="top",
legend.title=element_blank())+
coord_flip()

Resources