I have the following plot:
m <- structure(list(Var1 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("FE", "AG",
"NO", "SPH", "SEP", "H/I", "CMP"), class = "factor"), Var2 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L
), .Label = c("FE", "AG", "NO", "SPH", "SEP", "H/I", "CMP"), class = "factor"),
value = c(0, 0.0419753086419753, 0.172839506172839, 0.0740740740740741,
0.0123456790123457, 0.111111111111111, 0.0617283950617284,
0.0419753086419753, 0, 0.0765432098765432, 0.0246913580246914,
0.00493827160493827, 0.0567901234567901, 0.0320987654320988,
0.172839506172839, 0.0765432098765432, 0, 0.175308641975309,
0.0197530864197531, 0.177777777777778, 0.120987654320988,
0.0740740740740741, 0.0246913580246914, 0.175308641975309,
0, 0.00740740740740741, 0.0814814814814815, 0.0395061728395062,
0.0123456790123457, 0.00493827160493827, 0.0197530864197531,
0.00740740740740741, 0, 0.0197530864197531, 0.00987654320987654,
0.111111111111111, 0.0567901234567901, 0.177777777777778,
0.0814814814814815, 0.0197530864197531, 0, 0.0716049382716049,
0.0617283950617284, 0.0320987654320988, 0.120987654320988,
0.0395061728395062, 0.00987654320987654, 0.0716049382716049,
0), vtext = c("0.0%", "4.2%", "17.3%", "7.4%", "1.2%", "11.1%",
"6.2%", "4.2%", "0.0%", "7.7%", "2.5%", "0.5%", "5.7%", "3.2%",
"17.3%", "7.7%", "0.0%", "17.5%", "2.0%", "17.8%", "12.1%",
"7.4%", "2.5%", "17.5%", "0.0%", "0.7%", "8.1%", "4.0%",
"1.2%", "0.5%", "2.0%", "0.7%", "0.0%", "2.0%", "1.0%", "11.1%",
"5.7%", "17.8%", "8.1%", "2.0%", "0.0%", "7.2%", "6.2%",
"3.2%", "12.1%", "4.0%", "1.0%", "7.2%", "0.0%")), .Names = c("Var1",
"Var2", "value", "vtext"), row.names = c(NA, -49L), class = "data.frame")
library(ggplot2)
ggplot(data = m, aes(x = Var2, y = Var1, fill = value, label = vtext)) +
xlab("") + ylab("") +
geom_tile() +
geom_text() +
scale_fill_gradient(low="white", high="darkmagenta") +
# Sample code for subtitles: ggtitle(bquote(atop("Age distribution", atop(italic(.(subtitle)), ""))))
ggtitle(bquote(atop(.(title), atop(italic(.(subtitle)), "")))) +
theme(axis.text.y = element_text(size = 12), axis.text.x = element_text(size = 12),
axis.title = element_text(size = 16, face = "bold"),
plot.title = element_text(size = 20),
panel.background = element_rect(fill = "white"),
legend.key.size = unit(0.02, "npc"),
legend.text = element_text(size = 14),
legend.title = element_text(size = 16))
Which results in:
The problem, is that the text in the darker squares is difficult to read. Is it possible to change the text color based on the background color, so the text in the clear boxes is black and in the darker boxes is white?
Add these two code lines:
geom_text(aes(color = value > 0.1)) +
scale_color_manual(guide = FALSE, values = c("black", "white"))
Here text color depends on value (value > 0.1) and colors are specified with scale_color_manual.
For the output like this:
Related
I have some data in a CSV file that I made up in order to create dot plots of different distributions.
These are the made-up data:
structure(list(uniform = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
4, 4, 4, 4, 5, 5, 5, 5), left_skew = c(1L, 2L, 2L, 3L, 3L, 3L,
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), right_skew = c(5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L,
2L, 2L, 1L), trunc_uni_left = c(3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), trunc_uni_right = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L), trunc_norm_left = c(3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L), trunc_norm_right = c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L), bimodal = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), extreme_left = c(3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L), extreme_right = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L)), row.names = c(NA,
-20L), class = "data.frame")
The dot-plot works when there are 'observations' in each of the five categories on the x-axis. However, if there are values missing then it only reflects those categories. For instance, in one plot there are no 1s and 2s so the plot only shows categories 3, 4, and 5.
I've tried using scale_x_discrete to set the limits and breaks but this doesn't work.
Here is the code I used to plot the data:
ggplot(df, aes(x = trunc_uni_left))+
geom_point()+
geom_dotplot(method = "histodot", binwidth = 0.25, fill = 'red', dotsize = 0.75)+
labs(x = 'Rating Categories', y = 'Rating Frequency')+
theme_bw()+
ylim(0 , 20)+
scale_x_discrete(breaks = c ("0.5", "1", "1.5", "2", "2.5"),
labels = c ("1", "2", '3', '4', '5'),
limits = c ("1", "2", "3", "4", "5"))+
theme(panel.grid = element_blank(),
text = element_text(size = 16),
axis.text.x = element_text(size = 16),
axis.title.x = element_text(size = 16, margin = margin(t = 20)),
axis.title.y = element_text(size = 16, margin = margin(r = 20)),
legend.title= element_text(size = 16))
Is there something I can do in ggplot to achieve this? Or alternatively, can I create a data frame in R that would allow me to do this?
I'm not the best coder in the world as you may be able to tell so would much appreciate the help.
Thanks!
Your breaks don't match the data. The breaks should be 1:5 which are the numbers in your df and supply new labels if required. However, I'm guessing you don't want new labels (please correct) and you just want to control the x-axis limits? In which case you can just supply the limits while changing trunc_uni_left to a factor:
ggplot(df, aes(as.factor(trunc_uni_left))) +
geom_dotplot(method = "histodot", binwidth = 0.25, fill = 'red', dotsize = 0.75)+
labs(x = 'Rating Categories', y = 'Rating Frequency')+
theme_bw() +
scale_x_discrete(limits = seq(1, 5, 1))
If you did want to re-label the x-axis with bespoke labels make sure you match the breaks to what is actually in your data:
ggplot(df, aes(as.factor(trunc_uni_left))) +
geom_dotplot(method = "histodot", binwidth = 0.25, fill = 'red', dotsize = 0.75) +
labs(x = 'Rating Categories', y = 'Rating Frequency')+
theme_bw() +
scale_x_discrete(limits = seq(1, 5, 1),
breaks = seq(1, 5, 1),
labels = paste0("my_lab_", seq(1, 5, 1)))
In this example you don't need the breaks as the data happens to be ordered because it's numeric. But if you had some string as the input you would need to match the breaks and labels in the order you want them.
I have the following graph:
And would like to make what I thought would be a very simple change: I would like to remove the top, right and bottom sides of the left facet label border lines.
How do I do I remove those lines, or draw the equivalent of the right hand lines? I would rather not muck about with grobs, if possible, but won't say no to any solution that works.
Graph code:
library(ggplot2)
library(dplyr)
library(forcats)
posthoc1 %>%
mutate(ordering = -as.numeric(Dataset) + Test.stat,
Species2 = fct_reorder(Species2, ordering, .desc = F)) %>%
ggplot(aes(x=Coef, y=Species2, reorder(Coef, Taxa), group=Species2, colour=Taxa)) +
geom_point(size=posthoc1$Test.stat*.25, show.legend = FALSE) +
ylab("") +
theme_classic(base_size = 20) +
facet_grid(Taxa~Dataset, scales = "free_y", space = "free_y", switch = "y") +
geom_vline(xintercept = 0) +
theme(axis.text.x=element_text(colour = "black"),
strip.placement = "outside",
strip.background.x=element_rect(color = NA, fill=NA),
strip.background.y=element_rect(color = "black", fill=NA)) +
coord_cartesian(clip = "off") +
scale_x_continuous(limits=NULL)
Data:
structure(list(Dataset = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 5L, 5L, 5L, 5L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L), .Label = c("All.habitat", "Aut.habitat", "Habitat.season",
"Lit.season", "Spr.habitat"), class = "factor"), Species = structure(c(1L,
2L, 3L, 5L, 6L, 10L, 11L, 12L, 13L, 1L, 3L, 5L, 6L, 13L, 1L,
2L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L), .Label = c("Ar.sp1",
"Ar.sp2", "Arc.sp1", "B.pus", "Dal.sp1.bumps", "Dip.unID", "I.palladium",
"Pale", "Ph.sp3", "Port", "Somethus", "sty", "Sty.sp1"), class = "factor"),
Species2 = structure(c(2L, 9L, 1L, 4L, 5L, 7L, 11L, 12L,
13L, 2L, 1L, 4L, 5L, 13L, 2L, 9L, 4L, 5L, 6L, 10L, 8L, 7L,
11L, 13L), .Label = c("Arcitalitrus sp1", "Armadillidae sp1 ",
"Brachyiulus pusillus ", "Dalodesmidae sp1", "Diplopoda",
"Isocladosoma pallidulum ", "Ommatoiulus moreleti ", "Philosciidae sp2",
"Porcellionidae sp1", "Siphonotidae sp2", "Somethus sp1",
"Styloniscidae ", "Styloniscidae sp1"), class = "factor"),
Taxa = structure(c(3L, 3L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
1L, 2L, 2L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 3L), .Label = c("Amphipoda",
"Diplopoda", "Isopoda"), class = "factor"), Variable = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Autumn", "Litter",
"Spring", "Summer"), class = "factor"), Coef = c(1.911502938,
2.086917154, 1.571872993, 12.61184801, 15.6161116, -1.430032837,
-12.51944478, 12.33934516, -8.040249562, 8.08258816, 1.780142396,
12.88982576, 16.78107544, -13.22641153, 1.68810887, 2.093965381,
12.27209197, 15.08328526, -6.334640911, -11.29985948, -11.62658947,
-1.676293808, -6.246555908, -3.470297147), SE = c(0.403497472,
2.21607562, 0.348600794, 2.423896379, 0.509468128, 3.423013791,
2.382857733, 1.775086895, 2.087788334, 2.23631504, 0.33402261,
2.518562443, 0.459720131, 1.950974996, 0.2476205, 0.235648095,
1.815155489, 0.325804415, 2.564680067, 2.437104984, 2.212583358,
2.677618401, 2.324019051, 0.420436743), Test.stat = c(18.36532749,
13.27324683, 13.29039037, 20.50277493, 44.06097153, 10.55234932,
14.64951518, 13.22575401, 20.16415411, 16.55627107, 11.81407568,
15.15213717, 40.67205188, 12.62233207, 37.60085488, 16.90879258,
20.20215107, 80.30520371, 13.35250626, 13.01692428, 17.52987519,
20.03658771, 12.02467914, 53.5052683)), row.names = 10:33, class = "data.frame")
This solution is based on grobs: find positions of "strip-l" (left strips) and then substitute the rect grobs with line grobs.
p <- posthoc1 %>%
mutate(ordering = -as.numeric(Dataset) + Test.stat,
Species2 = fct_reorder(Species2, ordering, .desc = F)) %>%
ggplot(aes(x=Coef, y=Species2, reorder(Coef, Taxa), group=Species2, colour=Taxa)) +
geom_point(size=posthoc1$Test.stat*.25, show.legend = FALSE) +
ylab("") +
theme_classic(base_size = 20) +
facet_grid(Taxa~Dataset, scales = "free_y", space = "free_y", switch = "y") +
geom_vline(xintercept = 0) +
theme(axis.text.x=element_text(colour = "black"),
strip.placement = "outside",
#strip.background.x=element_rect(color = "white", fill=NULL),
strip.background.y=element_rect(color = NA)
) +
coord_cartesian(clip = "off") +
scale_x_continuous(limits=NULL)
library(grid)
q <- ggplotGrob(p)
lg <- linesGrob(x=unit(c(0,0),"npc"), y=unit(c(0,1),"npc"),
gp=gpar(col="red", lwd=4))
for (k in grep("strip-l",q$layout$name)) {
q$grobs[[k]]$grobs[[1]]$children[[1]] <- lg
}
grid.draw(q)
I would like to change the order of my legend, and not to display them in alphabetical order as you can see below. I would like to have
"NONE","LIGHT","MEDIUM","HEAVY","V_COLD","COLD","MEDIUM","HOT".
Is it possible? I tried with several arguments but without success.
Below, my table :
structure(list(SOUNAME = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "BALLYSHANNON (CATHLEENS FALL)", class = "factor"),
year_month = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L,
6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L), .Label = c("2013-03",
"2013-04", "2013-05", "2013-06", "2013-07", "2013-08", "2013-09",
"2013-10", "2013-12"), class = "factor"), pre_type = structure(c(4L,
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L), .Label = c("HEAVY", "LIGHT", "MEDIUM",
"NONE"), class = "factor"), pre_value = c(13L, 2L, 11L, 5L,
9L, 3L, 10L, 7L, 2L, 6L, 13L, 10L, 10L, 1L, 15L, 4L, 16L,
2L, 7L, 5L, 2L, 2L, 17L, 9L, 7L, 3L, 13L, 6L, 5L, 2L, 10L,
14L, 1L, 5L, 19L, 6L), tem_type = structure(c(4L, 3L, 2L,
1L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L,
2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L, 4L,
3L, 2L, 1L), .Label = c("COLD", "HOT", "MEDIUM", "V_COLD"
), class = "factor"), tem_value = c(0L, 7L, 0L, 23L, 0L,
29L, 0L, 1L, 0L, 29L, 2L, 0L, 0L, 21L, 9L, 0L, 0L, 5L, 25L,
0L, 0L, 18L, 13L, 0L, 0L, 21L, 9L, 0L, 0L, 26L, 5L, 0L, 0L,
24L, 0L, 7L), cnt_vehicle = c(NA, 2754406, NA, NA, NA, 2846039,
NA, NA, NA, 3149377, NA, NA, NA, 3058810, NA, NA, NA, 3362614,
NA, NA, NA, 3415716, NA, NA, NA, 3020812, NA, NA, NA, 3076665,
NA, NA, NA, 2775306, NA, NA), x = c(1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L,
6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L
)), .Names = c("SOUNAME", "year_month", "pre_type", "pre_value",
"tem_type", "tem_value", "cnt_vehicle", "x"), row.names = c(NA,
-36L), class = "data.frame")
Below my graph:
ggplot(data = b_complet_2013, aes(x = x, y = pre_value*100000, fill = pre_type), stat = "identity") +
scale_x_continuous(breaks=(1:9)+0.2, labels=unique(b_complet_2013$year_month)) +
geom_bar(stat = "identity", width=0.3) +
xlab("date") + ylab ("Number of days of précipitations(left) and temperatures (ritght)") +
ggtitle("Precipitation per month") +
geom_bar(data=b_complet_2013,aes(x=x+0.4, y=tem_value*100000, fill=tem_type), width=0.3, stat = "identity") +
xlab("date") + ylab("Number of days of precipitations(left) and temperatures (ritght)") +
ggtitle("Impact of weather on road traffics") + theme( axis.title.y = element_text(color = "blue", face = "bold")) +
theme(axis.text.y = element_text(color = "blue", face = "bold", size=9)) + theme( axis.title.y.right = element_text(color = "black", face = "bold")) +
theme(axis.text.y.right = element_text(color = "black", size = 9, face = "bold")) +
geom_line(mapping = aes(x= x+0.2, y = as.numeric(cnt_vehicle)), colour = I("blue"), size = 0.8) +
geom_point(aes(x= x+0.2, y = as.numeric(cnt_vehicle), colour = I("blue")), show.legend=FALSE, stat = "identity") +
scale_y_continuous(sec.axis = sec_axis(~./100000,name="Number of days of precipitations(left) and temperatures (ritght)")) +
theme( plot.title = element_text(size = 17)) + theme(axis.title.x = element_text(size = 12)) + theme(axis.title.y = element_text(size = 12)) +
labs(y = "Number of vehicles", color ="black") +
theme(panel.background = element_rect(linetype = "dashed", fill="white"), plot.background = element_rect(linetype = "dashed",fill="grey90" ))
I want to create a correlation plot using geom_tile(). This is a reproducible chunk of code:
library(ggplot2)
df.m <- structure(list(Trait = structure(c(6L, 5L, 1L, 3L, 2L, 9L, 4L,
10L, 11L, 7L, 8L, 6L, 5L, 1L, 3L, 2L, 9L, 4L, 10L, 11L, 7L, 8L,
6L, 5L, 1L, 3L, 2L, 9L, 4L, 10L, 11L, 7L, 8L), .Label = c("R1",
"R2", "R3", "R4", "R5",
"R6", "R7", "R8",
"R9", "R10",
"R11"), class = "factor"), Variable = structure(c(3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("C1", "C2", "C3"), class = "factor"),
value = c(0.967444360256195, 0.937379062175751, 0.647411823272705,
0.512605130672455, 0.50750744342804, 0.508640229701996, 0.508640229701996,
0.503542542457581, 0.442936152219772, 0.510905921459198,
0.504675328731537, NA, 0.834005177021027, 0.667642116546631,
0.579914391040802, 0.579344689846039, 0.536050498485565,
0.532062888145447, 0.52408766746521, 0.520099997520447, 0.504719197750092,
0.450031787157059, NA, NA, 0.571457028388977, 0.451690584421158,
0.354736804962158, 0.46138596534729, 0.477354824542999, 0.447128057479858,
0.287439465522766, 0.498456537723541, 0.508722245693207)), .Names = c("Trait",
"Variable", "value"), row.names = c(NA, -33L), class = "data.frame")
p <- ggplot(na.omit(df.m)) + aes(x = Variable, y = Trait, fill = value) +
geom_tile(colour = "white", size = 0.75) +
scale_colour_distiller(limits = c(-1, 1), direction = -1, palette = "RdBu", name = "Coefficient") +
scale_size(range = c(0,20), name = title, guide = "none") +
geom_text(data = df.m, aes(x = Variable, y = Trait, label = sprintf("%.2f", value)), size = 4, inherit.aes = FALSE) +
theme(plot.title = element_text(size = 20), axis.text.y = element_text(size = 10),
axis.text.x = element_text(size = 10, angle = 90, hjust = 1, vjust = 0.5),
legend.position = "bottom", legend.margin = unit(1.0, "cm"),
legend.text = element_text(size = 8), legend.title = element_text(size = 10),
legend.key.size = unit(1.0, "cm"),
panel.background = element_rect(fill = "white")) +
xlab("\nVariables 1") + ylab("Variables 2\n")
plot(p)
However, the tiles are filled with their own color gradient scale and not with the one defined bye scale_colour_distiller(). Moreover, both key legends appear in the plot:
I just want the tiles to be colored according to the gradient defined by the scale_colour_distiller(). How can I do that? From where geom_plot() is sucking the color scale?
Thanks in advance!
You need to use scale_fill_distiller since you are mapping value to the fill aesthetic and not to the color aesthetic.
I have a dataframe called split2_data (actually a drop-leveled subset of a bigger data frame).
It contains a column "Loci", which are factors that I want as x-axes, and several columns of y-values (note: All of these values are <=1) that I would like to plot beside one another in their respective x factor.
The dataframe
structure(list(Loci = structure(1:8, .Label = c("C485", "C487_PigTa",
"C536", "Carey", "Cool", "Coyote", "Deadpool", "Epstein"), class = "factor"),
All = structure(c(5L, 6L, 7L, 1L, 2L, 4L, 3L, 8L), .Label = c("0.0246",
"0.0352", "0.0563", "0.0646", "0.2349", "0.3242", "0.3278",
"0.6854"), class = "factor"), X1_only = structure(c(4L, 3L,
2L, 1L, 6L, 6L, 6L, 5L), .Label = c("0.0133", "0.7292", "0.8586",
"0.9377", "0.961", "1"), class = "factor"), X78_only = structure(c(7L,
6L, 4L, 5L, 8L, 3L, 1L, 2L), .Label = c("0.0018", "0.0175",
"0.4958", "0.6055", "0.7472", "0.7563", "0.825", "1"), class = "factor"),
X8_removed = structure(c(5L, 6L, 8L, 1L, 2L, 3L, 4L, 7L), .Label = c("0.0181",
"0.0348", "0.1482", "0.1706", "0.2217", "0.2602", "0.6748",
"0.7123"), class = "factor"), X8_only = structure(c(6L, 7L,
3L, 8L, 5L, 4L, 1L, 2L), .Label = c("0.1266", "0.1945", "0.4389",
"0.4496", "0.7078", "0.709", "0.8882", "1"), class = "factor"),
X7_removed = structure(c(6L, 4L, 5L, 2L, 1L, 3L, 7L, 8L), .Label = c("0.0159",
"0.02", "0.0541", "0.3232", "0.3972", "0.4226", "0.4919",
"0.5951"), class = "factor"), X7_only = structure(c(3L, 4L,
7L, 5L, 6L, 8L, 1L, 2L), .Label = c("0.0082", "0.1759", "0.4957",
"0.5248", "0.6665", "0.6789", "0.8372", "1"), class = "factor"),
X5_removed = structure(c(5L, 7L, 6L, 1L, 3L, 4L, 2L, 8L), .Label = c("0.0195",
"0.0316", "0.08", "0.1069", "0.1549", "0.395", "0.4405",
"0.6298"), class = "factor"), X5_only = structure(c(1L, 2L,
6L, 7L, 3L, 5L, 7L, 4L), .Label = c("0.0871", "0.2022", "0.3532",
"0.3677", "0.5292", "0.7602", "1"), class = "factor"), X4_removed = structure(c(8L,
4L, 7L, 2L, 3L, 5L, 1L, 6L), .Label = c("0.0188", "0.0194",
"0.0511", "0.1716", "0.1862", "0.6454", "0.661", "0.8003"
), class = "factor"), X4_only = structure(c(2L, 5L, 1L, 6L,
7L, 3L, 8L, 4L), .Label = c("0.0026", "0.0378", "0.2884",
"0.4386", "0.5116", "0.6549", "0.6928", "1"), class = "factor"),
X3_removed = structure(c(5L, 7L, 6L, 1L, 2L, 3L, 4L, 8L), .Label = c("0.0612",
"0.0627", "0.0808", "0.1636", "0.2728", "0.477", "0.5307",
"0.6506"), class = "factor"), X3_only = structure(c(8L, 1L,
7L, 2L, 4L, 6L, 3L, 5L), .Label = c("0.0225", "0.2111", "0.2471",
"0.5087", "0.6294", "0.768", "0.8263", "0.8951"), class = "factor"),
X2_removed = structure(c(4L, 5L, 6L, 3L, 7L, 2L, 1L, 8L), .Label = c("0.0526",
"0.0608", "0.0854", "0.2036", "0.3168", "0.3668", "0.413",
"0.7608"), class = "factor"), X2_only = structure(c(5L, 3L,
6L, 4L, 2L, 8L, 1L, 7L), .Label = c("-", "0.0014", "0.0949",
"0.1637", "0.1818", "0.5521", "0.8585", "1"), class = "factor"),
X1_removed = structure(c(5L, 7L, 3L, 6L, 1L, 4L, 2L, 8L), .Label = c("0.0258",
"0.031", "0.0496", "0.0676", "0.1053", "0.1439", "0.2823",
"0.5465"), class = "factor")), .Names = c("Loci", "All",
"X1_only", "X78_only", "X8_removed", "X8_only", "X7_removed",
"X7_only", "X5_removed", "X5_only", "X4_removed", "X4_only",
"X3_removed", "X3_only", "X2_removed", "X2_only", "X1_removed"
), row.names = 9:16, class = "data.frame")
I can't think of how to do this in base R, and after some careful study of other questions here, this is the best that I can come up with:
library(reshape)
library(ggplot2)
require(ggplot2)
split2_datam<-melt(split2_data,id="Loci")
p2<- ggplot(split2_datam, aes(x =Loci, y = value, color = variable, width=.15)) + geom_bar(position="dodge") + ylab("P-value")+ geom_hline(yintercept=0.05)+ opts(axis.text.x = theme_text(angle=90, size=8)) + scale_y_discrete(breaks=seq(0,1)) + scale_fill_grey()
p2
#when I add stat="identity", the y values don't change- they just shrink relative to the x-axis
p2<- ggplot(split2_datam, aes(x =Loci, y = value, color = variable, width=.15)) + geom_bar(position="dodge", stat="identity") + ylab("P-value")+ geom_hline(yintercept=0.05)+ opts(axis.text.x = theme_text(angle=90, size=8)) + scale_y_discrete(breaks=seq(0,1)) + scale_fill_grey()
p2
The plot:
You'll notice that the different variables are often much greater than 1. They should not be. Any idea what's causing this/how to fix?
Other things I don't yet know how to do/fix (perhaps this question should be cross-referenced?):
I don't know why the greyscale isn't working
I don't know how to make the legend scale correctly with the plot
I don't understand why my columns have an 'X' appended to them (e.g. "X1_only" instead of "1_only")
Thank you so much in advance for any suggestions!
Your data have been read in as factors, probably because there are some "-" characters mixed in with your data.
You'll want to convert them to NA when you read in your data using na.strings = "-".