A plot with different colors and pch - r

I have this table:
structure(list(Samples = structure(1:7, .Label = c("Sample1",
"Sample2", "sample3", "sample4", "sample5", "sample6", "sample7"
), class = "factor"), number_cycle = c(22L, 22L, 26L, 26L, 26L,
22L, 22L), Quality = structure(c(2L, 2L, 3L, 1L, 1L, 1L, 3L), .Label = c("Bad",
"Good", "Middle"), class = "factor"), Concentration = c(14L,
24L, 22L, 40L, 10L, 27L, 12L), Raw_reads = c(100000L, 5000L,
70000L, 340000L, 4789L, 50000L, 25000L)), class = "data.frame", row.names = c(NA,
-7L))
I would like to do a scatter plot (raw_reads ~ concentration) which I can observe the dispersion of two factors: quality (colors) and number of cycles (pch). When I try this code, I have a plot without any point.
palette(rainbow(3))
plot(test$Raw_reads ~ test$Concentration,
col = test$Quality,
pch = test$number_cycle)
legend(x = "topleft",
legend = levels(test$Quality),
col = rainbow(3),
pch = test$number_cycle)
So what can I do to obtain my scatter plot?
Thank you for your help.

when you use base R plot, for colors and pch,
you need to specify a vector that is as long as your datapoints. For pch, you need to specify valid numbers 1:20 i think and for colours, either a factor, or characters. One way to get around is to call out a predefined pch or col vector:
palette(rainbow(3))
PCH = c(18,19)
names(PCH) = as.character(unique(test$number_cycle))
COL = palette(rainbow(3))
names(COL) = unique(test$Quality)
plot(test$Raw_reads,test$Concentration,
col = COL[test$Quality], pch = PCH[as.character(test$number_cycle)])
legend(x = "topleft", legend = names(COL), fill = COL)
legend(x = 80000,y=42, legend = names(PCH), col = "black",pch = PCH)
You also need two legends, one for color, one for pch.

Related

Add Count Labels on Top of Barchart in base R

I have a barplot to which I'm looking to add count labels on top of each bars. Can someone tell me how to do that in base R and NOT using ggplot2?
Since I saw your last question I have a bit more detail than others.
Example data
structure(list(ID = c(140L, 620L, 868L, 1120L, 2313L), DemAffl = c(10L,
4L, 5L, 10L, 11L), DemAge = c(76L, 49L, 70L, 65L, 68L), DemCluster = c(16L,
35L, 27L, 51L, 4L), DemClusterGroup = c("C", "D", "D", "F", "A"
), DemGender = c("U", "U", "F", "M", "F"), DemReg = c("Midlands",
"Midlands", "Midlands", "Midlands", "Midlands"), DemTVReg = c("Wales & West",
"Wales & West", "Wales & West", "Midlands", "Midlands"), PromClass = c("Gold",
"Gold", "Silver", "Tin", "Tin"), PromSpend = c(16000, 6000, 0.02,
0.01, 0.01), PromTime = c(4L, 5L, 8L, 7L, 8L), TargetBuy = c(0L,
0L, 1L, 1L, 0L), TargetAmt = c(0L, 0L, 1L, 1L, 0L)), row.names = c(NA,
5L), class = "data.frame")
To make the plot
counts <- table(df$TargetBuy)
Here you will need to change the y-axis scale because if you don't the top label wont show
b <- barplot(counts, main= "number of yes/no", xlab = "response", ylab = "number of occurrences", ylim=c(0,4))
To add the labels you need to add text() to the plot
text(x= b, y=counts,pos = 3, label = counts, cex = 0.8, col = "red")
so the full thing will look like this
counts <- table(df$TargetBuy)
b <- barplot(counts, main= "number of yes/no", xlab = "response", ylab = "number of occurrences", ylim=c(0,4)
text(x= b, y=counts,pos = 3, label = counts, cex = 0.8, col = "red")
This produces a plot that looks like this. Note that I changed the y axis length to 4. If it was set to 3, the top 3 above the first bar would not show

Adding a ggtree object to already existing ggplot with shared y-axis

I have the following data and plot:
Data:
structure(list(type = c("mut", "mut", "mut", "mut", "mut", "mut",
"mut", "mut", "gene", "gene", "gene", "gene"), gene = c("gyrA",
"gyrA", "gyrB", "gyrB", "parC", "parC", "parE", "parE", "qnrA1",
"qnrA1", "sul3", "sul3"), type2 = c(1, 1, 1, 1, 1, 1, 1, 1, 2,
2, 2, 2), id = c("2014-01-7234-1-S", "2015-01-3004-1-S", "2014-01-2992-1-S",
"2016-17-299-1-S", "2015-01-2166-1-S", "2014-01-4651-1-S", "2016-02-514-2-S",
"2016-02-402-2-S", "2016-02-425-2-S", "2015-01-5140-1-S", "2016-02-522-2-S",
"2016-02-739-2-S"), result = c("1", "0", "0", "0", "0", "0",
"1", "1", "0", "0", "0", "1"), species = c("Broiler", "Pig",
"Broiler", "Red fox", "Pig", "Broiler", "Wild bird", "Wild bird",
"Wild bird", "Pig", "Wild bird", "Wild bird"), fillcol = c("Broiler_1",
"Pig_0", "Broiler_0", "Red fox_0", "Pig_0", "Broiler_0", "Wild bird_1",
"Wild bird_1", "Wild bird_0", "Pig_0", "Wild bird_0", "Wild bird_1"
)), row.names = c(NA, -12L), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), vars = "gene", drop = TRUE, indices = list(
0:1, 2:3, 4:5, 6:7, 8:9, 10:11), group_sizes = c(2L, 2L,
2L, 2L, 2L, 2L), biggest_group_size = 2L, labels = structure(list(
gene = c("gyrA", "gyrB", "parC", "parE", "qnrA1", "sul3")), row.names = c(NA,
-6L), class = "data.frame", vars = "gene", drop = TRUE, indices = list(
0:1, 2:3, 4:5, 6:7, 8:9, 10:11), group_sizes = c(2L, 2L,
2L, 2L, 2L, 2L), biggest_group_size = 2L, labels = structure(list(
gene = c("gyrA", "gyrB", "parC", "parE", "qnrA1", "sul3")), row.names = c(NA,
-6L), class = "data.frame", vars = "gene", drop = TRUE)))
Plot:
library(ggplot2)
p1 <- ggplot(test_df, aes(fct_reorder(gene, type2),
factor(id),
fill = fillcol,
alpha = result)) +
geom_tile(color = "white")+
theme_minimal()+
labs(fill = NULL)+
theme(axis.text.x = element_text(angle = 90,
hjust = 1,
vjust = 0.3,
size = 7),
axis.title = element_blank(),
panel.grid = element_blank(),
legend.position = "right")+
guides(alpha = FALSE)+
coord_fixed()
Additionally, I have the following tree object:
structure(list(edge = structure(c(23L, 23L, 22L, 22L, 21L, 21L,
20L, 20L, 19L, 19L, 18L, 18L, 17L, 17L, 16L, 16L, 15L, 15L, 14L,
14L, 13L, 13L, 1L, 3L, 2L, 9L, 22L, 23L, 4L, 5L, 20L, 21L, 11L,
12L, 18L, 19L, 10L, 17L, 8L, 16L, 6L, 7L, 14L, 15L), .Dim = c(22L,
2L)), edge.length = c(2, 2, 0, 0, 2.5, 0.5, 2, 2, 0.75, 0.25,
0.5, 0.5, 2.41666666666667, 0.166666666666667, 3.0625, 0.145833333333333,
3.38888888888889, 0.326388888888889, 3, 3, 0.5, 0.111111111111111
), tip.label = c("2016-02-425-2-S", "2016-02-522-2-S", "2015-01-2166-1-S",
"2016-02-402-2-S", "2016-02-514-2-S", "2016-17-299-1-S", "2016-02-739-2-S",
"2015-01-5140-1-S", "2014-01-2992-1-S", "2014-01-7234-1-S", "2014-01-4651-1-S",
"2015-01-3004-1-S"), Nnode = 11L), class = "phylo", order = "postorder")
Which is plotted like this:
library(ggtree)
p2 <- ggtree(tree)+
geom_treescale()+
geom_tiplab(align = TRUE, linesize = 0, size = 1)+
xlim(0, 4.2)
What I want to do is to combine the tree and the first plot, and order the first plot y-axis after the order in the tree, so that they match. I have tried to use some of the solutions here, but I can't seem to produce the same plot with the facet_plot function. Is there a way to identify maching values on the y-axis on both plots, and then combine them?
This is how I want it to look (approximately):
We need to arrange the tile plot in the same order as the tree plot and then we need to lay the two plots out so they correspond. The first task is relatively straightforward, but I'm not sure how to do the second without some manual tweaking of the layout.
library(tidyverse)
library(ggtree)
library(grid)
library(gridExtra)
p2 <- ggtree(tree)+
geom_treescale()+
geom_tiplab(align = TRUE, linesize = 0, size = 3)+
xlim(0, 4.2)
Now that we've created the tree plot, let's get the ordering of the y axis programmatically. We can do that using ggplot_build to get the plot structure.
p2b = ggplot_build(p2)
We can look at the data for the plot layout by running p2b$data in the console. This outputs a list with the various data frames that represent the plot structure. Looking these over, we can see that the fifth and six data frames have the node labels. We'll use the fifth one (p2b$data[[5]] and order them based on the y column to get a vector of node labels (p2b$data[[5]] %>% arrange(y) %>% pull(label))). Then we'll convert test_df$id to a factor variable with this node ordering.
test_df = test_df %>%
mutate(id = factor(id, levels=p2b$data[[5]] %>% arrange(y) %>% pull(label)))
(As another option, you can get the ordering of the nodes directly from p2 with p2$data %>% filter(isTip) %>% arrange(parent) %>% pull(label))
Now we can generate the tile plot p1 with a node order that corresponds to that of the tree plot.
p1 <- ggplot(test_df, aes(fct_reorder(gene, type2),
factor(id),
fill = fillcol,
alpha = result)) +
geom_tile(color = "white")+
theme_minimal()+
labs(fill = NULL)+
theme(axis.text.x = element_text(angle = 90,
hjust = 1,
vjust = 0.3,
size = 7),
axis.title = element_blank(),
panel.grid = element_blank(),
legend.position = "right")+
guides(alpha = FALSE)+
coord_fixed()
We can see in the plot below that the labels correspond.
grid.arrange(p2, p1, ncol=2)
Now we need to lay out the two plots with only one set of labels and with the node lines matching up vertically with the tiles. I've done this with some manual tweaking below by creating a nullGrob() (basically a blank space below p1) and adjusting the heights argument to get the alignment. The layout can probably be done programmatically, but that would take some additional grob (graphical object) manipulation.
grid.arrange(p2 + theme(plot.margin=margin(0,-20,0,0)),
arrangeGrob(p1 + theme(axis.text.y=element_blank()),
nullGrob(),
heights=c(0.98,0.02)),
ncol=2)

igraph pie vertices with defined segment sizes and continuous colors for each segment

I'm trying to create a pie chart in igraph for R, where each vertex is graphed as a pie chart separated into two equal-sized segments and each segment is colored as a gradient from blue to white to red based on two columns. I can create the color gradients for each, but I'm having difficulty figuring out how to map a color gradient to a pie chart. All of the examples I'm finding color pie chart segments based on discrete values, not continuous.
Edit: I've incorporated more information here to create a reproducible example.
library(igraph)
test1 <- structure(list(From.Molecule.s. = c("EIF2AK2", "ELK1", "FOS"),
To.Molecule.s. = c("CHUK", "FOS", "CD14"), Relationship.Type = structure(c(1L,
1L, 2L), .Label = c("activation", "expression", "inhibition",
"phosphorylation", "protein-DNA interactions", "protein-protein interactions",
"protein-RNA interactions", "reaction", "transcription"), class = "factor"),
color = c("Light Coral", "Light Coral", "Goldenrod")), .Names = c("From.Molecule.s.", "To.Molecule.s.", "Relationship.Type", "color"), row.names = c(20L, 23L, 27L), class = "data.frame")
test1_exp <- structure(list(From.Molecule.s. = c("CD14", "CHUK", "ECSIT",
"EIF2AK2", "ELK1", "FOS"), expression.x = c(-0.454, 0.863, -0.326,
0, -0.31, 0), Expr.p.value.x = c(0.198648, 0.003975, 0.164683,
NA, 0.039658, NA), vector_colors.x = structure(c(1L, 11L, 3L,
27L, 5L, 27L), .Label = c("#0000FF", "#2626FF", "#4848FF", "#4D4DFF",
"#4F4FFF", "#5C5CFF", "#9A9AFF", "#F9F9FF", "#FF0000", "#FF1B1D",
"#FF2426", "#FF3C40", "#FF5459", "#FF565B", "#FF585D", "#FF6369",
"#FF7076", "#FF757C", "#FF777E", "#FF7E86", "#FF8992", "#FF9AA3",
"#FF9FA9", "#FFA5AF", "#FFAEB9", "#FFB6C1", "#FFFFFF"), class = "factor"),
expression.y = c(0.271, -0.022, 0.219, 0, 0.126, 0), Expr.p.value.y = c(0.705028,
0.97643, 0.63173, NA, 0.670641, NA), vector_colors.y = structure(c(17L,
15L, 21L, 28L, 23L, 28L), .Label = c("#0000FF", "#4242FF",
"#5A5AFF", "#7676FF", "#7B7BFF", "#9797FF", "#9D9DFF", "#A7A7FF",
"#ACACFF", "#C6C6FF", "#D0D0FF", "#D8D8FF", "#EFEFFF", "#F2F2FF",
"#F4F4FF", "#FF0000", "#FF282A", "#FF2A2C", "#FF3134", "#FF4044",
"#FF4448", "#FF5257", "#FF777E", "#FF8B94", "#FF8F98", "#FFB0BB",
"#FFB6C1", "#FFFFFF"), class = "factor")), .Names = c("From.Molecule.s.",
"expression.x", "Expr.p.value.x", "vector_colors.x", "expression.y",
"Expr.p.value.y", "vector_colors.y"), row.names = c(NA, 6L), class = "data.frame")
TLR <- graph_from_data_frame(test1, directed = T,vertices = test1_exp)
V(TLR)$size=7.5
V(TLR)$label.cex=0.8
V(TLR)$label.dist=0.75
V(TLR)$label.degree = pi/2
V(TLR)$label.color = "black"
TLR_net <- plot(TLR,edge.arrow.size = 0.15,vertex.shape = "pie",vertex.pie.color = c(V(TLR)$vector_colors.x,V(TLR)$vector_colors.y))
And what the test output looks like now:

Order axis when doing a bubble chart using plotly in R

I have a bubble chart using plotly in R but the order of the axis appear to be somehow odd.
The output is as follows and you can see how the axis are not correct:
The code that I'm using is as follows
library(plotly)
library(ggplot2)
file <- c("C://link//data.csv")
#dataSource <- read.csv(file, sep =",", header = TRUE)
dataSource <- read.table(file, header=T, sep=",")
dataSource <- na.omit(dataSource)
slope <- 1
dataSource$size <- sqrt(dataSource$Y.1 * slope)
colors <- c('#4AC6B7', '#1972A4') #, '#965F8A', '#FF7070', '#C61951')
plot_ly(dataSource,
x = ~Y.1.vs.Y.2,
y = ~YTD.vs.Y.1.YTD,
color = ~BU,
size = ~size,
colors = colors,
type = 'scatter',
mode = 'markers',
sizes = c(min(dataSource$size), max(dataSource$size)),
marker = list(symbol = 'circle', sizemode = 'diameter',
line = list(width = 2, color = '#FFFFFF')),
text = ~paste('Business Unit:',
BU, '<br>Product:',
Product, '<br>Y.1.vs.Y.2:',
Y.1.vs.Y.2, '<br>YTD.vs.Y.1.YTD:',
YTD.vs.Y.1.YTD)) %>%
layout(title = 'Y.1.vs.Y.2 v. YTD.vs.Y.1.YTD',
xaxis = list(title = 'Y.1.vs.Y.2',
gridcolor = 'rgb(255, 255, 255)',
zerolinewidth = 1,
ticklen = 5,
gridwidth = 2),
yaxis = list(title = 'YTD.vs.Y.1.YTD',
gridcolor = 'rgb(255, 255, 255)',
zerolinewidth = 1,
ticklen = 5,
gridwith = 2),
paper_bgcolor = 'rgb(243, 243, 243)',
plot_bgcolor = 'rgb(243, 243, 243)')
The data is as follows:
structure(list(BU = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("B", "D"), class = "factor"), Product = structure(c(4L, 5L, 7L, 8L, 9L, 13L, 1L, 3L, 4L, 11L, 12L, 13L), .Label = c("ADT", "BHL", "CEX", "CMX", "CTL", "HTH", "MTL", "SSL", "TLS", "UTV", "WEX", "WLD", "WMX"), class = "factor"), Y.2 = c(4065L, 499L, 20L, 5491L, 781L, 53L, 34L, 1338L, 557L, 428L, 310L, 31L), Y.1 = c(4403L, 550L, 28L, 5225L, 871L, 46L, 22L, 1289L, 602L, 426L, 318L, 37L), Y.1.YTD = c(4403L, 550L, 28L, 5225L, 871L, 46L, 22L, 1289L, 602L, 426L, 318L, 37L), YTD = c(5026L, 503L, 29L, 3975L, 876L, 40L, 62L, 1395L, 717L, 423L, 277L, 35L), Y.1.vs.Y.2 = structure(c(12L, 7L, 11L, 4L, 8L, 1L, 2L, 3L, 12L, 6L, 10L, 9L), .Label = c("-13%", "-35%", "-4%", "-5%", "-76%", "0%", "10%", "12%", "19%", "3%", "40%", "8%"), class = "factor"), YTD.vs.Y.1.YTD = structure(c(8L, 5L, 11L, 3L, 7L, 2L, 9L, 12L, 10L, 1L, 2L, 4L), .Label = c("-1%", "-13%", "-24%", "-5%", "-9%", "0%", "1%", "14%", "182%", "19%", "4%", "8%"), class = "factor")), .Names = c("BU", "Product", "Y.2", "Y.1", "Y.1.YTD", "YTD", "Y.1.vs.Y.2", "YTD.vs.Y.1.YTD"), row.names = c(2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 13L, 14L, 15L), class = "data.frame", na.action = structure(c(1L, 7L, 12L), .Names = c("1", "7", "12"), class = "omit"))
Any ideas on how can I order the axis properly?
Thanks
There are a few ways to manipulate factor levels, but things can get a bit messy if you're not careful. You should familiarize yourself with ?levels and ?factor, as well as maybe ?reorder, ?relevel
In the meantime, try something like this
dataSource[[7]] <- factor(dataSource[[7]], levels = c("-76%", "-35%", "-13%", "-5%", "-4%", "0%", "3%", "8%", "10%", "12%", "19%", "40%"))
Edit
To consolidate my answer and comment...
This behaviour is caused because of the way factors are encoded. Your axes are strings and factor order is determined alphnumerically. So to change their order you have to specify it as above, or else code them numerically and give them the required names. There are many different ways to change them, in several packages. This answer provides a standard base R method for handling factors. For further info start with the manual pages I suggested.
As for it being "very manual", since factors are categorical (and therefore have a potentially arbitrary order), there is no way to automate their order unless you code them numerically in the desired order.
Thanks to the comments above I've been able to resolve the issue. Find below the full code, which I hope might help other users:
library(plotly)
library(ggplot2)
file <- c("C://link//data.csv")
dataSource <- read.table(file, header=T, sep=",")
dataSource <- na.omit(dataSource)
# Additional code to format the input values and recalculate the percentages
BUValues = dataSource$BU
ProductValues = dataSource$Product
dataSource <- as.data.frame(data.matrix(dataSource), stringsAsfactors = FALSE)
dataSource$BU = BUValues
dataSource$Product = ProductValues
dataSource$Y.1.vs.Y.2 = round((dataSource$Y.1/dataSource$Y.2 -1)*100,2)
dataSource$YTD.vs.Y.1.YTD = round((dataSource$YTD/dataSource$Y.1.YTD -1)*100,2)
slope <- 1
dataSource$size <- sqrt(dataSource$Y.1 * slope)
colors <- c('#4AC6B7', '#1972A4') #, '#965F8A', '#FF7070', '#C61951')
plot_ly(dataSource,
x = ~Y.1.vs.Y.2,
y = ~YTD.vs.Y.1.YTD,
color = ~BU,
size = ~size,
colors = colors,
type = 'scatter',
mode = 'markers',
sizes = c(min(dataSource$size), max(dataSource$size)),
marker = list(symbol = 'circle', sizemode = 'diameter',
line = list(width = 2, color = '#FFFFFF')),
text = ~paste('Business Unit:', BU,
'<br>Product:', Product,
'<br>YoY:',Y.1.vs.Y.2,
'<br>YTD:',YTD.vs.Y.1.YTD)) %>%
layout(title = 'YoY vs YTD Performance',
xaxis = list(title = 'YoY Performance (%)',
gridcolor = 'rgb(255, 255, 255)',
zerolinewidth = 1,
ticklen = 5,
gridwidth = 2),
yaxis = list(title = 'YTD Performance (%)',
gridcolor = 'rgb(255, 255, 255)',
zerolinewidth = 1,
ticklen = 5,
gridwith = 2),
paper_bgcolor = 'rgb(243, 243, 243)',
plot_bgcolor = 'rgb(243, 243, 243)')

geom_point not really hollow, error bar overlaps

This might seem a really stupid mistake on my part but whenever I specify geom_point depending on a factor and choose a hollow point and a solid point (shapes 1 and 19), and plot error bars, it crosses the point.
Here are my data frames:
> dput(head(allbins.sum))
structure(list(T = c(0L, 0L, 10L, 10L, 20L, 20L), treatment = structure(c(1L,
2L, 1L, 2L, 1L, 2L), .Label = c("control bead", "dP bead"), class = "factor"),
N = c(3, 3, 3, 3, 3, 3), cellsBase = c(0, 0, 0.013028995209506,
0.135599858885737, -0.0130289952095061, 0.759359209760127
), sd = c(0, 0, 0.0597063567767786, 0.0469731690178533, 0.0983667566897066,
0.183436089048999), se = c(0, 0, 0.034471481157405, 0.0271199717771474,
0.0567920734541125, 0.105906875391532), ci = c(0, 0, 0.148318812500416,
0.116687820597672, 0.244356569875469, 0.455680506502609),
bin = c("BinA", "BinA", "BinA", "BinA", "BinA", "BinA")), .Names = c("T",
"treatment", "N", "cellsBase", "sd", "se", "ci", "bin"), row.names = c(NA,
6L), class = "data.frame")
> dput(head(allbins.fitdata))
structure(list(wellvidbin = structure(c(1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("A1-002-BinA", "A1-002-BinB", "A1-002-BinC", "A1-031-BinA",
"A1-031-BinB", "A1-031-BinC", "A3-004-BinA", "A3-004-BinB", "A3-004-BinC",
"B1-032-BinA", "B1-032-BinB", "B1-032-BinC", "B4-026-BinA", "B4-026-BinB",
"B4-026-BinC", "C4-027-BinA", "C4-027-BinB", "C4-027-BinC"), class = "factor"),
treatment = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("control bead",
"dP bead"), class = "factor"), wellvid = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("A1-002", "A1-031", "A3-004",
"B1-032", "B4-026", "C4-027"), class = "factor"), bin = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("BinA", "BinB", "BinC"), class = "factor"),
T = c(0L, 10L, 20L, 30L, 40L, 50L), T.factor = structure(1:6, .Label = c("0",
"10", "20", "30", "40", "50", "60"), class = "factor"), cells = c(7L,
11L, 26L, 27L, 28L, 36L), cellsS = c(-1.36568429306349, -1.20296446240061,
-0.592765097414793, -0.552085139749072, -0.511405182083351,
-0.185965520757582), cellsBase = c(0, 0.162719830662884,
0.772919195648701, 0.813599153314422, 0.854279110980143,
1.17971877230591), treatT = structure(c(2L, 4L, 6L, 8L, 10L,
12L), .Label = c("control bead.0", "P bead.0", "control bead.10",
"P bead.10", "control bead.20", "P bead.20", "control bead.30",
"P bead.30", "control bead.40", "P bead.40", "control bead.50",
"P bead.50", "control bead.60", "P bead.60"), class = "factor"),
fit = c(0.0285939715820639, 0.304399288764407, 0.58020460594675,
0.856009923129092, 1.13181524031144, 1.40762055749378), se.fit = c(0.157415367032567,
0.132348142293459, 0.114707848741265, 0.108190467052118,
0.114707848741265, 0.132348142293459), upr = c(0.337128090965895,
0.563801647659587, 0.805031989479629, 1.06806323855124, 1.35664262384431,
1.66702291638896), lwr = c(-0.279940147801767, 0.0449969298692267,
0.35537722241387, 0.643956607706942, 0.906987856778556, 1.1482181985986
)), .Names = c("wellvidbin", "treatment", "wellvid", "bin",
"T", "T.factor", "cells", "cellsS", "cellsBase", "treatT", "fit",
"se.fit", "upr", "lwr"), class = c("data.table", "data.frame"
), row.names = c(NA, -6L), .internal.selfref = <pointer: 0x0000000000100788>)
And the code:
ggplot(data=allbins.sum, aes(x=T, y=cellsBase, shape=treatment)) + geom_point(size=5, aes(shape=treatment))+
geom_errorbar(aes(ymin=cellsBase-se, ymax=cellsBase+se), width=2, size=1) +
geom_smooth(data=allbins.fitdata, size=1, aes(y=fit, ymin=lwr, ymax=upr),
color="black", method="lm", stat="identity", alpha=0.2)+
facet_grid(bin~.) +
scale_shape_manual(values=c(1, 19))
This gives me this plot:
Any hints on how to have the hollow circles to be hollowed?
I also tried specifying geom_shape (aes(fill=treatment) and then scale_fill_manual but then it is also applied to my geom_smooth
Thanks for the help!
If you mean that you don't want the line of the error bar to be visible through the 'hollow' points, then plot geom_errorbar first, then plot geom_point second, with solid fill, so it will overlay the error bar.
ggplot(data=allbins.sum, aes(x=T, y=cellsBase)) +
# plotting this first
geom_errorbar(aes(ymin=cellsBase-se, ymax=cellsBase+se), width=2, size=1) +
# plotting this second, with a hollow fillable shape, and black outline
geom_point(size=5, shape = 21, color='black',
aes(fill = treatment)) +
# solid black and solid white fill for the points
scale_fill_manual(values = c('black', 'white')) +
theme_bw()
(The data you posted only has these points for allbins.sum, and the code for allbins.fitdata has an error, so no error bars on this plot)

Resources