enter link description hereDummy Data Sheet
I'm having issues with getting my graphs to display error bars, even though I've copied and modified code that has worked just fine.
This is the code and graph that is working:
graph2 <- summary %>%
filter(Accuracy == "FALSE") %>%
ggplot(.) + aes(x = Author, y = mean_err, fill = reorder(Quality, -mean_err)) +
geom_bar(stat = "summary", fun.y = "mean", position = "dodge") +
xlab("Race of Author") + ylab("Proportion Lure Reported") + labs(fill = "Text Quality") +
theme(panel.grid.major = element_blank(), panel.grid.minor =
element_blank(),panel.background = element_blank(), axis.line = element_line(colour =
"black")) +
geom_errorbar(aes(ymin= mean_err - se_error, ymax= mean_err + se_error),
position=position_dodge(width=0.9), width=.1) +
geom_text(aes(label=round(mean_err,digits=2)), position=position_dodge(width=.9),
vjust=5) + scale_fill_manual(values=wes_palette("GrandBudapest2"))```
This prints out a working graph with error bars.
When I tried to modify the code to have it instead show the proportion of unsure answers reported, I used this code:
filter(Accuracy == "TRUE") %>%
ggplot(.) + aes(x = Author, y = mean_unsu, fill= Author) +
geom_bar(stat = "summary", fun.y = mean, position = "dodge") +
xlab("Race of Author") + ylab("Proportion Unsure Reported") +
theme(panel.grid.major = element_blank(), panel.grid.minor =
element_blank(),panel.background = element_blank(), axis.line = element_line(colour =
"black")) +
geom_errorbar(aes(ymin= mean_unsu - se_unsu, ymax= mean_unsu + se_unsu),
position=position_dodge(width=0.9), width=.1) +
geom_text(aes(label=round(mean_err,digits=2)), position=position_dodge(width=.9),
vjust=5) + scale_fill_manual(values=wes_palette("GrandBudapest2"))
I get this image with no error bars.
I'm very confused at what I'm doing wrong. I feel like it is probably something very simple.
Related
How can I add a trend line that represents the average values for the groups in red and black? I have tried geom_line() and geom_smooth() and haven't gotten it to work.
Here is the code I used to make this graph:
ggplot(data = long_data, aes(x = day, y = val, group = ID, colour = outcome)) +
geom_line(aes(color = as.factor(outcome), linetype = as.factor(outcome)), size = 0.7) +
scale_linetype_manual(values = c("longdash", "solid")) +
scale_color_manual(values = c("black", "red")) + theme_bw() +
theme(legend.position = "", panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
labs(x = "day", y = var, colour = "outcome ") +
xlim(0, 25) +
ggtitle(var) +
theme(plot.title = element_text(hjust = 0.5)) +
ylim(0,100)
Your grouping variable is ID, that's what geom_smooth will inherit.
Try specifying your desired grouping variable outcome by using geom_smooth(aes(group = outcome))
Trouble with overlapping geom_text labels on pie chart
library(scales)
blank_theme <- theme_minimal()+
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.border = element_blank(),
panel.grid=element_blank(),
axis.ticks = element_blank(),
plot.title=element_text(size=14, face="bold")
)
df6 <- data.frame(group = c("Seedling","Ground", "Fern", "Moss"),value = c(2.125,80.125, 11.376,6.375))
# Create a basic bar
pie6 = ggplot(df6, aes(x="", y=value, fill=group)) + geom_bar(stat="identity", width=1) + blank_theme + theme(axis.text.x=element_blank())
# Convert to pie (polar coordinates) and add labels
pie6 = pie6 + coord_polar("y", start=0) + geom_text(aes( label = paste0((value), "%")),position = position_stack(vjust = 0.5))
# Remove labels and add title
pie6 = pie6 + labs(x = NULL, y = NULL, fill = NULL, title = "Understory Composition, Site 2")
# Tidy up the theme
pie6 = pie6 + theme_classic() + theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5, color = "black"))
pie6
This is my current output. How can i change the labels such that they do not overlap? i have tried various hjust and vjust, positions and position_stack but to no avail
Any help appreciated. thank you.
A manual approach to this problem could be to manipulate the orientation of the text by setting the angle parameter in the geom_text function. This allows you to set the orientation of all text fragments at once by assigning a singel value to angle. You may also set the angle for the individual text items as illustrated below.
ggplot(df6, aes(x = "", y = value, fill = group)) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y", start = 0) +
blank_theme +
theme(axis.text.x = element_blank()) +
geom_text(aes(label = paste0((value), "%")),
position = position_stack(vjust = 0.5),
angle = c(-90, -90, 0, -90)) ####### here
The code below adjust the angle depending on the pie it describes.
ggplot(df6, aes(x = "", y = value, fill = group)) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y", start = 0) +
blank_theme +
theme(axis.text.x = element_blank()) +
geom_text(aes(label = paste0((value), "%")),
position = position_stack(vjust = 0.5),
angle = c(-97, -110, 0, -70)) ####### here
Another way to work around the problem you state, is to play with the start parameter of the coord_polar function:
ggplot(df6, aes(x = "", y = value, fill = group)) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y", start = 180) + ####### here
blank_theme +
theme(axis.text.x = element_blank()) +
geom_text(aes(label = paste0((value), "%")) ,
position = position_stack(vjust = 0.5))
Have you tried using the package ggrepel? If so I recommend. It automatically stops labels overlapping and finds a fit for each other them.
#Install ggrepel package
install.packages("ggrepel", dependencies=TRUE)
df6 <- data.frame(group = c("Seedling","Ground", "Fern", "Moss"),value = c(2.125,80.125, 11.376,6.375))
# Create a basic bar
pie6 = ggplot(df6, aes(x="", y=value, fill=group)) + geom_bar(stat="identity",
width=1) + blank_theme + theme(axis.text.x=element_blank())
# Convert to pie (polar coordinates) and add labels using ggrepel
pie6 = pie6 + coord_polar("y", start=0) + ggrepel::geom_text_repel(aes( label =
paste0((value), "%")),position = position_stack(vjust = 0.5))
# Remove labels and add title
pie6 = pie6 + labs(x = NULL, y = NULL, fill = NULL, title = "Understory Composition, Site 2")
# Tidy up the theme
pie6 = pie6 + theme_classic() + theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5, color = "black"))
pie6
I've seen similar questions but the answers did not seem to help my graph. I have a plot here:
n_cases_plot <- ggplot(data = n_cases, aes(x = month, y = n_cases, color
= "#668cff", fill = "#668cff")) +
geom_bar(stat = "identity") +
theme(panel.background = element_blank()) +
ggtitle("Number of Parole Cases Heard by Month") +
theme(plot.title = element_text(hjust = 0.4)) +
guides(fill=FALSE) +
guides(color = FALSE) +
xlab("") +
ylab("") +
theme(text=element_text(size=12, family="Georgia")) +
geom_smooth(method = lm, se = FALSE, aes(color = "lightskyblue1")) +
theme(panel.background = element_rect(fill = "moccasin")) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
I'm trying to add text to describe the decrease as -29%. I've tried adding
+ annotate("text", x = 4, y = 10, "-29%")
But I keep getting the error
Error: Invalid input: date_trans works with objects of class Date only
I am working with a dataset that I found on wikipedia regarding the nutritional content of staple grains. I scraped the data table using the rvest package and created the graphic shown below
It was pointed out to me that perhaps it might be better to represent the "Recommended Dietary Allowance"(RDA) with a vertical line as opposed to a bar.
1) How to a create the separate vertical line representing "Recommended Dietary Allowance"?
The code used to create the graphic is below: I am not sure on whether I should include the code used to gather and wrangle the data. Please let me know if that would help.
ggplot(grain.nut, aes(grain, nutrients, fill = grain)) +
facet_wrap(~ nutrient.component., scales = "free") +
geom_bar(stat = "identity", position = "dodge") +
coord_flip() +
labs(title = "Nutrient Content of Major Staple Foods per 100 gram Portion",
caption = "https://en.wikipedia.org/wiki/Staple_food#Nutritional_content") +
theme(plot.title = element_text(size = 30, face = "bold")) +
theme(axis.text.y = element_blank()) +
theme(axis.ticks.y = element_blank()) +
theme(panel.grid.major.y = element_blank()) +
theme(panel.grid.minor.y = element_blank()) +
theme(axis.title = element_blank()) +
theme(legend.position = c(0.80,0.05), legend.direction = "horizontal") +
theme(legend.title = element_blank()) +
theme(plot.caption = element_text(hjust = 0.84)) +
guides(fill=guide_legend(reverse=TRUE)) +
scale_fill_manual(values = c("#e70000",
"#204bcc",
"#68ca3b",
"#fe9bff",
"#518901",
"#de0890",
"#fcba4c",
"#292c7a",
"#e69067",
"#79b5ff",
"#68272d",
"#c9cb6c"))
I have tried using geom_vline as well as geom_hline. But I think my problem is the way I am trying to call the value for RDA via levels(grain.nut$grain)1, the output of which is "Recommended Dietary Allowance".
geom_vline(aes(xintercept = levels(grain.nut$grain)[1]))
Any help would be appreciated!
Here is an approach using geom_linerange or geom_pointrange.
First the data:
library("rvest")
library(tidyverse)
url <- "https://en.wikipedia.org/wiki/Staple_food"
nutrient <- url %>%
read_html() %>%
html_nodes(xpath='//*[#id="mw-content-text"]/div/table[2]') %>%
html_table()
get the correct order of levels for discrete scale:
lev = levels(as.factor(z$grain))[c(1:4,6:12, 5)]
The plot:
ggplot() +
geom_col(data = nutrient[[1]] %>%
as.tibble() %>%
gather(grain, value, 2:ncol(.)) %>%
filter(grain!="RDA") %>%
mutate(nutrient = `Nutrient component:`,
value = as.numeric(value)), aes(grain, value, fill = grain), position = "dodge")+
geom_pointrange(data = nutrient[[1]] %>%
as.tibble() %>%
gather(grain, value, 2:ncol(.)) %>%
filter(grain=="RDA") %>%
mutate(nutrient = `Nutrient component:`,
value = as.numeric(value)), aes(x = grain, ymin = 0, ymax = value, y = value, color = grain), size = 0.3, show.legend = F)+
facet_wrap(~ nutrient, scales = "free") +
scale_x_discrete(limits = lev) +
coord_flip() +
labs(title = "Nutrient Content of Major Staple Foods per 100 gram Portion",
caption = "https://en.wikipedia.org/wiki/Staple_food#Nutritional_content") +
theme(plot.title = element_text(size = 30, face = "bold")) +
theme(axis.text.y = element_blank()) +
theme(axis.ticks.y = element_blank()) +
theme(panel.grid.major.y = element_blank()) +
theme(panel.grid.minor.y = element_blank()) +
theme(axis.title = element_blank()) +
theme(legend.position = c(0.80,0.05), legend.direction = "horizontal") +
theme(legend.title = element_blank()) +
theme(plot.caption = element_text(hjust = 0.84)) +
guides(fill=guide_legend(reverse=TRUE)) +
scale_fill_manual(values = c("#e70000",
"#204bcc",
"#68ca3b",
"#fe9bff",
"#518901",
"#de0890",
"#fcba4c",
"#292c7a",
"#e69067",
"#79b5ff",
"#68272d",
"#c9cb6c"))
Basically two layers are used with different data: geom_col with data without RDA and geom_pointrange for data with only RDA. And the order is changed in scale_x_discrete to match the lev object.
If you do not like the points use geom_linerange and omit the y in he aes call
or did u mean this?
ggplot() +
geom_col(data = nutrient[[1]] %>%
as.tibble() %>%
gather(grain, value, 2:ncol(.)) %>%
filter(grain!="RDA") %>%
mutate(nutrient = `Nutrient component:`,
value = as.numeric(value)), aes(grain, value, fill = grain), position = "dodge")+
geom_hline(data = nutrient[[1]] %>%
as.tibble() %>%
gather(grain, value, 2:ncol(.)) %>%
filter(grain=="RDA") %>%
mutate(nutrient = `Nutrient component:`,
value = as.numeric(value)), aes(yintercept = value), show.legend = F)+
facet_wrap(~ nutrient, scales = "free") +
coord_flip() +
labs(title = "Nutrient Content of Major Staple Foods per 100 gram Portion",
caption = "https://en.wikipedia.org/wiki/Staple_food#Nutritional_content") +
theme(plot.title = element_text(size = 30, face = "bold")) +
theme(axis.text.y = element_blank()) +
theme(axis.ticks.y = element_blank()) +
theme(panel.grid.major.y = element_blank()) +
theme(panel.grid.minor.y = element_blank()) +
theme(axis.title = element_blank()) +
theme(legend.position = c(0.80,0.05), legend.direction = "horizontal") +
theme(legend.title = element_blank()) +
theme(plot.caption = element_text(hjust = 0.84)) +
guides(fill=guide_legend(reverse=TRUE)) +
scale_fill_manual(values = c("#e70000",
"#204bcc",
"#68ca3b",
"#fe9bff",
"#518901",
"#de0890",
"#fcba4c",
"#292c7a",
"#e69067",
"#79b5ff",
"#68272d",
"#c9cb6c"))
I made ton of figures with ggplot 0.8.9 (which is what I'm still running). Now I need to modify these figures to include legends. I am running across all sorts of problems that are hard to solve because I am getting really confused about theme and opts and many SO answers that apply to later versions.
At this point, it seems like I need to update ggplot2 and rewrite all of my code just so I can have legends on my figures. Is this true? I've read the ggplot2 transition guide, it makes it seem true.
Here's what the old code looks like (does not produce a legend): And here is the data for the sake of reproducibility: mean10v2 and stderr10.
me10<-read.table("mean10v2.txt", header=TRUE)
se10<-read.table("stderr10.txt", header=TRUE)
ggplot() +
geom_ribbon(aes(x = me10[me10$trt=="CC", "tu"], ymin=(me10[me10$trt=="CC", "biomassA"]-
se10[se10$trt=="CC", "biomassA"]), ymax=(me10[me10$trt=="CC",
"biomassA"]+se10[se10$trt=="CC", "biomassA"])), alpha=0.25) +
geom_line(aes(me10[me10$trt=="CC", "tu"], y=me10[me10$trt=="CC", "biomassA"]), size=1)+
geom_ribbon(aes(x = me10[me10$trt=="PF", "tu"], ymin=(me10[me10$trt=="PF", "biomassA"]-
se10[se10$trt=="PF", "biomassA"]), ymax=(me10[me10$trt=="PF",
"biomassA"]+se10[se10$trt=="PF", "biomassA"])), alpha=0.25) +
geom_line(aes(me10[me10$trt=="PF", "tu"], y=me10[me10$trt=="PF", "biomassA"]),
colour="red2", linetype="dashed", size=1) +
geom_ribbon(aes(x = me10[me10$trt=="P", "tu"], ymin=(me10[me10$trt=="P", "biomassA"]-
se10[se10$trt=="P", "biomassA"]), ymax=(me10[me10$trt=="P",
"biomassA"]+se10[se10$trt=="P", "biomassA"])), alpha=0.25) +
geom_line(aes(me10[me10$trt=="P", "tu"], y=me10[me10$trt=="P", "biomassA"]),
colour="blue3", linetype="dotted", size=1) +
opts(panel.grid.minor = theme_blank()) +
opts(panel.grid.major = theme_blank()) +
opts(panel.background = theme_blank()) +
opts(axis.line = theme_segment()) +
opts(legend.position=c(.5,.5)) +
opts(axis.title.x = theme_text(size=12,vjust=-0.5)) +
opts(axis.title.y = theme_text(size=12,angle=90)) +
opts(axis.text.x = theme_text(colour="black", size=16)) +
opts(axis.text.y = theme_text(colour="black", size=16)) +
annotate("text", x = -Inf, y = Inf, label = "a", face="bold", hjust = -5, vjust=2, size
= 9) +
ylab("") +
xlab("") +
ylim(0,2200)
Updating the theme parts is actually quite simple. You really just need to change opts() to theme() and replace theme_* with element_*. Some other names have changed, like you'll use element_line instead of theme_segment.
But more generally, you're using ggplot all wrong:
my_df <- me10[,c('trt','tu','biomassA')]
my_se <- setNames(se10[,c('trt','tu','biomassA')],c('trt','tu','se'))
my_df <- merge(my_df,my_se)
ggplot(data = my_df,aes(x = tu,y = biomassA)) +
geom_ribbon(aes(group = trt,ymin = biomassA - se,ymax = biomassA + se),alpha = 0.25) +
geom_line(aes(group = trt,linetype = trt,colour = trt)) +
labs(x = "",y = "") +
ylim(0,2200) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(),
legend.position=c(.5,.5),
axis.title.x = element_text(size=12,vjust=-0.5),
axis.title.y = element_text(size=12,angle=90),
axis.text.x = element_text(colour="black", size=16),
axis.text.y = element_text(colour="black", size=16)) +
annotate("text", x = -Inf, y = Inf, label = "a", face="bold", hjust = -5, vjust=2, size = 9)
Notice how much cleaner that is, and putting the data into an appropriate form only took three lines. Also note that there is absolutely no need to keep repeating the opts() or theme() calls for every....single....thing...you....set.
And then if you want to choose specific colors/linetypes for each group, you do that using the scale functions, not by setting them individually:
+ scale_colour_manual(values = c('black','red2','blue3')) +
scale_linetype_manual(values = c('solid','dashed','dotted'))