I am trying to fill with the same colour as the lines the data of the histograms shown in the figure below, I am using the following code. I have tried many things using fill, scale_fill_manual but without success. Any idea in how to correct this?
(stations = unique(DSF_moments$Station))
(station_cols = scales::hue_pal()(length(stations)))
(names(station_cols) = sort(stations))
for (i in 1:length(listDF2))
{
df1 <- as.data.frame(listDF2[[i]])
df1[is.na(df1)] <- 0
plot1 <- ggplot(df1, aes(x = Date, y = DailyMeanStreamflow, colour=Station)) +
geom_line(size = 1, show.legend = FALSE) +
geom_point(size=1.5, shape=21, fill="white",na.rm = TRUE, show.legend = FALSE)+
labs(title = "Daily Mean Streamflow", y = "Q[m3/s/Day]", x = "Date") +
theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(size=11)) +
scale_color_manual(values = station_cols)
plot2 <- ggplot(df1, aes(DailyMeanStreamflow, colour=Station)) +
geom_histogram(show.legend = FALSE) +
labs(title = "Daily Mean Streamflow Histogram", y = "Frequency", x="Q[m3/s/Day]")+
scale_colour_manual(values = station_cols) + scale_fill_manual(values = station_cols)
(Monthly_Streamflow_Station <- df1 %>% group_by(month) %>% summarise(Monthly_Streamflow_Station = mean(DailyMeanStreamflow, na.rm=TRUE)))
plot3 <- ggplot(Monthly_Streamflow_Station, aes(x = month, y = Monthly_Streamflow_Station, colour=unique(df1$Station))) +
geom_line(size = 1, show.legend = FALSE) +
geom_point(size=1.5, shape=21, fill="white",na.rm = TRUE, show.legend = FALSE)+
labs(title = "Monthly Mean Streamflow", y = "Q[m3/s/Month]", x = "Month") +
theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(size=11)) +
scale_x_continuous (breaks=seq(1,12,by=1)) +
scale_color_manual(values = station_cols)
plot4 <- ggplot(Monthly_Streamflow_Station, aes(Monthly_Streamflow_Station, colour=unique(df1$Station))) +
geom_histogram(show.legend = FALSE) +
labs(title = "Monthly Mean Streamflow Histogram", y = "Frequency", x="Q[m3/s/Month]") +
scale_colour_manual(values = station_cols)
(Annual_Streamflow_Station <- df1 %>% group_by(year) %>% summarise(Annual_Streamflow_Station = mean(DailyMeanStreamflow, na.rm=TRUE)))
plot5 <- ggplot(Annual_Streamflow_Station, aes(x = year, y = Annual_Streamflow_Station, colour=unique(df1$Station))) +
geom_line(size = 1, show.legend = FALSE) +
geom_point(size=1.5, shape=21, fill="white",na.rm = TRUE, show.legend = FALSE)+
labs(title = "Annual Mean Streamflow", y = "Q[m3/s/Year]", x = "Year") +
theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(size=11)) +
scale_color_manual(values = station_cols)
plot6 <- ggplot(Annual_Streamflow_Station, aes(Annual_Streamflow_Station,colour=unique(df1$Station))) +
geom_histogram(show.legend = FALSE) +
labs(title = "Annual Mean Streamflow Histogram", y = "Frequency", x="Q[m3/s/Year]") +
scale_colour_manual(values = station_cols)
grid.arrange(grobs=list(plot1, plot2, plot3, plot4, plot5, plot6), ncol = 2, nrow = 3)
name5<- paste("Plots","_", siteNumber[i], ".png", sep="")
g <- arrangeGrob(plot1, plot2, plot3, plot4, plot5, plot6, ncol = 2, nrow = 3)
ggsave(g,filename = name5,width=22,height=11,units="in",dpi=500)
dev.off()
}
Try this change on your loop. No output produced due to lack of data. I have also changed scale_color_*() by scale_fill_*() where necesssary as said by great #aosmith that histograms require filling option enabled:
#Code
for (i in 1:length(listDF2))
{
df1 <- as.data.frame(listDF2[[i]])
df1[is.na(df1)] <- 0
plot1 <- ggplot(df1, aes(x = Date, y = DailyMeanStreamflow, colour=Station)) +
geom_line(size = 1, show.legend = FALSE) +
geom_point(size=1.5, shape=21, fill="white",na.rm = TRUE, show.legend = FALSE)+
labs(title = "Daily Mean Streamflow", y = "Q[m3/s/Day]", x = "Date") +
theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(size=11)) +
scale_color_manual(values = station_cols)
plot2 <- ggplot(df1, aes(DailyMeanStreamflow, fill=Station)) +
geom_histogram(show.legend = FALSE) +
labs(title = "Daily Mean Streamflow Histogram", y = "Frequency", x="Q[m3/s/Day]")+
scale_fill_manual(values = station_cols)
(Monthly_Streamflow_Station <- df1 %>% group_by(month) %>% summarise(Monthly_Streamflow_Station = mean(DailyMeanStreamflow, na.rm=TRUE)))
plot3 <- ggplot(Monthly_Streamflow_Station, aes(x = month, y = Monthly_Streamflow_Station, colour=unique(df1$Station))) +
geom_line(size = 1, show.legend = FALSE) +
geom_point(size=1.5, shape=21, fill="white",na.rm = TRUE, show.legend = FALSE)+
labs(title = "Monthly Mean Streamflow", y = "Q[m3/s/Month]", x = "Month") +
theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(size=11)) +
scale_x_continuous (breaks=seq(1,12,by=1)) +
scale_color_manual(values = station_cols)
plot4 <- ggplot(Monthly_Streamflow_Station,
aes(Monthly_Streamflow_Station,
fill=unique(df1$Station))) +
geom_histogram(show.legend = FALSE) +
labs(title = "Monthly Mean Streamflow Histogram", y = "Frequency", x="Q[m3/s/Month]") +
scale_fill_manual(values = station_cols)
(Annual_Streamflow_Station <- df1 %>% group_by(year) %>% summarise(Annual_Streamflow_Station = mean(DailyMeanStreamflow, na.rm=TRUE)))
plot5 <- ggplot(Annual_Streamflow_Station, aes(x = year, y = Annual_Streamflow_Station, colour=unique(df1$Station))) +
geom_line(size = 1, show.legend = FALSE) +
geom_point(size=1.5, shape=21, fill="white",na.rm = TRUE, show.legend = FALSE)+
labs(title = "Annual Mean Streamflow", y = "Q[m3/s/Year]", x = "Year") +
theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(size=11)) +
scale_color_manual(values = station_cols)
plot6 <- ggplot(Annual_Streamflow_Station,
aes(Annual_Streamflow_Station,
fill=unique(df1$Station))) +
geom_histogram(show.legend = FALSE) +
labs(title = "Annual Mean Streamflow Histogram", y = "Frequency", x="Q[m3/s/Year]") +
scale_fill_manual(values = station_cols)
grid.arrange(grobs=list(plot1, plot2, plot3, plot4, plot5, plot6), ncol = 2, nrow = 3)
name5<- paste("Plots","_", siteNumber[i], ".png", sep="")
g <- arrangeGrob(plot1, plot2, plot3, plot4, plot5, plot6, ncol = 2, nrow = 3)
ggsave(g,filename = name5,width=22,height=11,units="in",dpi=500)
dev.off()
}
Related
I'm trying to recreate this waffle bar chart: https://github.com/hrbrmstr/waffle#waffle-bar-charts-with-scales
library(dplyr)
library(waffle)
storms %>%
filter(year >= 2010) %>%
count(year, status) -> storms_df
ggplot(storms_df, aes(fill = status, values = n)) +
geom_waffle(color = "white", size = .25, n_rows = 10, flip = TRUE) +
facet_wrap(~year, nrow = 1, strip.position = "bottom") +
scale_x_discrete() +
scale_y_continuous(labels = function(x) x * 10, # make this multiplyer the same as n_rows
expand = c(0,0)) +
ggthemes::scale_fill_tableau(name=NULL) +
coord_equal() +
labs(
title = "Faceted Waffle Bar Chart",
subtitle = "{dplyr} storms data",
x = "Year",
y = "Count"
) +
theme_minimal(base_family = "Roboto Condensed") +
theme(panel.grid = element_blank(), axis.ticks.y = element_line()) +
guides(fill = guide_legend(reverse = TRUE))
And it seems that geom_waffle is no longer available, it's waffle now and they changed some arguments as well.
So I created a named vector and fixed the color argument, but it's still not working:
storms %>%
filter(year >= 2010) %>%
count(status) -> storms_df2
vec = extract2(storms_df2, 'n') %>% set_names(storms_df2$status)
ggplot(storms_df, aes(fill = status, values = n)) +
waffle(vec,colors=c("red","green","blue"), size = .25, rows = 10, flip = TRUE) +
facet_wrap(~year, nrow = 1, strip.position = "bottom") +
scale_x_discrete() +
scale_y_continuous(labels = function(x) x * 10, # make this multiplyer the same as n_rows
expand = c(0,0)) +
ggthemes::scale_fill_tableau(name=NULL) +
coord_equal() +
labs(
title = "Faceted Waffle Bar Chart",
subtitle = "{dplyr} storms data",
x = "Year",
y = "Count"
) +
theme_minimal(base_family = "Roboto Condensed") +
theme(panel.grid = element_blank(), axis.ticks.y = element_line()) +
guides(fill = guide_legend(reverse = TRUE))
What am I missing? The waffle funstion on it's own is working, but I need a bar chart by year:
waffle(vec, rows = 50, colors=c("red","green","blue"))
If you'll install waffle from this repository you'll be able to create this chart
install.packages("waffle", repos = "https://cinc.rud.is")
library(tidyverse)
library(waffle)
library(ggthemes)
storms %>%
filter(year >= 2010) %>%
count(year, status) -> storms_df
ggplot(storms_df, aes(fill = status, values = n)) +
geom_waffle(color = "white", size = .25, rows = 10, flip = TRUE) +
facet_wrap(~year, nrow = 1, strip.position = "bottom") +
scale_x_discrete() +
scale_y_continuous(labels = function(x) x * 10, # make this multiplyer the same as n_rows
expand = c(0,0)) +
ggthemes::scale_fill_tableau(name=NULL) +
coord_equal() +
labs(
title = "Faceted Waffle Bar Chart",
subtitle = "Created by Anil Goyal",
x = "Year",
y = "Count"
) +
theme_minimal(base_family = "Roboto Condensed") +
theme(panel.grid = element_blank(), axis.ticks.y = element_line()) +
guides(fill = guide_legend(reverse = TRUE))
I have changed the subtitle just to show that it is still working.
So, I have the two dataframes that produces two ggplots with the same facet that I want to combine
The first dataframe produces the following ggplot
Dataframe1
library(ggh4x)
library(ggnomics)
library(ggplot2)
library(data.table)
#dataframe
drug <- c("DrugA","DrugB1","DrugB2","DrugB3","DrugC1","DrugC2","DrugC3","DrugC4")
PR <- c(18,430,156,0,60,66,113,250)
GR <- c(16,425,154,0,56,64,111,248)
PS <- c(28,530,256,3,70,76,213,350)
GS <- c(26,525,254,5,66,74,211,348)
group<-c("n=88","n=1910","n=820","n=8","n=252","n=280","n=648","n=1186")
class<-c("Class A","Class B","Class B","Class B","Class C","Class C","Class C","Class C")
df <-data.frame(drug,group, class,PR,GR,PS,GS)
#make wide to long df
df.long <- melt(setDT(df), id.vars = c("drug","group","class"), variable.name = "type")
#Order of variables
df.long$type <- factor(df.long$type, levels=c("PR","GR","PS","GS"))
df.long$class <- factor(df.long$class, levels= c("Class B", "Class A", "Class C"))
df.long$group <- factor(df.long$group, levels= c("n=1910","n=820","n=8","n=88","n=252","n=280","n=648","n=1186"))
df.long$drug <- factor(df.long$drug, levels= c("DrugB1","DrugB2","DrugB3","DrugA","DrugC1","DrugC2","DrugC3","DrugC4"))
Ggplot for dataframe 1
ggplot(df.long, aes(fill = type, x = drug, y = value)) +
geom_bar(aes(fill = type), stat = "identity", position = "dodge", colour="white") +
geom_text(aes(label = value), position = position_dodge(width = 1.2), vjust = -0.5)+
scale_fill_manual(values = c("#fa9fb5","#dd1c77","#bcbddc","756bb1")) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 600)) +
theme(title = element_text(size = 18),
legend.text = element_text(size = 12),
axis.text.x = element_text(size = 9),
axis.text.y =element_text(size = 15),
plot.title = element_text(hjust = 0.5)) +
ggh4x::facet_nested(.~class + group, scales = "free_x", space= "free_x")
This is the 2nd dataframe
#dataframe 2
drug <- c("DrugA","DrugB1","DrugB2","DrugB3","DrugC1","DrugC2","DrugC3","DrugC4")
Sens <- c(0.99,0.97,NA,0.88,0.92,0.97,0.98,0.99)
Spec <- c(1,0.99,1,0.99,0.99,0.99,0.99,1)
class<-c("Class A","Class B","Class B","Class B","Class C","Class C","Class C","Class C")
df2 <-data.frame(drug,class,Sens,Spec)
#wide to long df2
df2.long <- melt(setDT(df2), id.vars = c("drug","class"), variable.name = "type")
#additional variables
df2.long$UpperCI <- c(0.99,0.99,NA,0.98,0.98,0.99,0.99,0.99,1,1,1,1,1,1,1,1)
df2.long$LowerCI <- c(0.97,0.98,NA,0.61,0.83,0.88,0.93,0.97,0.99,0.99,0.99,0.99,0.98,0.99,0.99,0.99)
#order of variables
df2.long$class <- factor(df2.long$class, levels= c("Class B", "Class A", "Class C"))
Ggplot for dataframe 2
ggplot(df2.long, aes(x=drug, y=value, group=type, color=type)) +
geom_line() +
geom_point()+
geom_errorbar(aes(ymin=LowerCI, ymax=UpperCI), width=.2,
position=position_dodge(0.05)) +
scale_y_continuous(labels=scales::percent)+
labs(x="drug", y = "Percentage")+
theme_classic() +
scale_color_manual(values=c('#999999','#E69F00')) +
theme(legend.text=element_text(size=12),
axis.text.x=element_text(size=9),
axis.text.y =element_text(size=15),
panel.background = element_rect(fill = "whitesmoke"))+
facet_wrap(facets = vars(class),scales = "free_x")
So I am trying to combine the two plots under the one facet (the one from dataframe 1), and so far I have done the following
ggplot(df.long)+
aes(x=drug, y=value,fill = type)+
geom_bar(, stat = "identity", position = "dodge", colour="white") +
geom_text(aes(label=value), position=position_dodge(width=0.9), vjust=-0.5, size=2) +
scale_fill_manual(breaks=c("PR","GR","PS","GS"),
values=c("#dd1c77","#756bb1","#fa9fb5","#e7e1ef","black","black")) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 1100),sec.axis=sec_axis(~./10, labels = function(b) { paste0(b, "%")},name="Percentage")) + #remove space between x axis labels and bottom of chart
theme(legend.text=element_text(size=12),
legend.position = 'bottom',
axis.text.x=element_text(size=9),
axis.text.y =element_text(size=15),
panel.background = element_rect(fill = "whitesmoke"), #color of plot background
panel.border = element_blank(), #remove border panels of each facet
strip.background = element_rect(colour = NA)) + #remove border of strip
labs(y = "Number of isolates", fill = "")+
geom_errorbar(data=df2.long,aes(x=drug, y=value*1000,ymin=LowerCI*1000, ymax=UpperCI*1000,color=type), width=.2,
position=position_dodge(0.05))+
geom_point(data=df2.long,aes(x=drug,y=value*1000,color=type),show.legend = F)+
geom_line(data=df2.long, aes(x=drug, y=value*1000, group=type, color=type)) +
scale_color_manual(values=c('#999999','#E69F00'))
but I'm stuck on adding the facet from the plot1. I hope anyone can help :)
For this specific case, I don't think the nested facets are the appropriate solution as the n = ... seems metadata of the x-axis group instead of a subcategory of the classes.
Here is how you could plot the data with facet_grid() instead:
ggplot(df.long, aes(drug, value, fill = type)) +
geom_col(position = "dodge") +
geom_text(aes(label = value),
position = position_dodge(0.9),
vjust = -0.5, size = 2) +
geom_errorbar(data = df2.long,
aes(y = value * 1000, color = type,
ymin = LowerCI * 1000, ymax = UpperCI * 1000),
position = position_dodge(0.05), width = 0.2) +
geom_point(data = df2.long,
aes(y = value * 1000, color = type),
show.legend = FALSE) +
geom_line(data = df2.long,
aes(y = value * 1000, group = type, color = type)) +
scale_fill_manual(breaks = c("PR", "GR", "PS", "GS"),
values=c("#dd1c77","#756bb1","#fa9fb5","#e7e1ef","black","black")) +
scale_color_manual(values=c('#999999','#E69F00')) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 1100),
sec.axis = sec_axis(~ ./10,
labels = function(b) {
paste0(b, "%")
}, name = "Percentage")) +
scale_x_discrete(
labels = levels(interaction(df.long$drug, df.long$group, sep = "\n"))
) +
facet_grid(~ class, scales = "free_x", space = "free_x") +
theme(legend.text=element_text(size=12),
legend.position = 'bottom',
axis.text.x=element_text(size=9),
axis.text.y =element_text(size=15),
panel.background = element_rect(fill = "whitesmoke"), #color of plot background
panel.border = element_blank(), #remove border panels of each facet
strip.background = element_rect(colour = NA))
If you insist on including the n = ... labels, perhaps a better way is to add these as text somehwere, i.e. adding the following:
stat_summary(fun = sum,
aes(group = drug, y = stage(value, after_stat = -50),
label = after_stat(paste0("n = ", y))),
geom = "text") +
And setting the y-axis limits to c(-100, 1000) for example.
Based on the following data frame and plot, I would like to conditionally change the colour of data points to black when did.it=="y". The shape of the dots and the colour of the lines, however, should remain unchanged. How can I do that?
set.seed(4887)
Strain <- rep(c(rep("A", times = 2), rep("B", times = 4)), times = 2)
Sex_ID <- rep(c("M_1", "F_2", "M_3", "F_4", "M_5", "F_6"), times = 2)
State <- rep(c("virgin", "mated", "expecting", "parent"), each = 6)
Huddling <- runif(8, 1.5, 3.8)
did.it<-rep(c("y","n","n"), times=8)
d <- data.frame(Strain, Sex_ID, State, Huddling, did.it)
library(tidyr)
d <- d %>%
separate(Sex_ID, c('Sex', 'ID'), sep = '_')
ggplot(d, aes(x = factor(State), y = Huddling, color = Sex, group = ID, shape = ID))+
facet_grid(Strain ~ ., scales = 'free_y') +
geom_point(size = 3, position = position_dodge(width=0.3), show.legend = F) +
geom_line(size = 0.7, position = position_dodge(width=0.3)) +
scale_color_manual(values = c('red4', 'midnightblue')) +
scale_fill_manual(values = "white") +
scale_x_discrete(limits = c("virgin", "mated", "expecting", "parent"),
labels = c("Virgin", "Mated", "Expecting", "Parent")) +
labs(y = "Time huddling (s)", x = "Reproductive stage") +
theme_classic() +
theme(axis.line.x = element_line(color = "black", size = 1),
axis.line.y = element_line(color = "black", size = 1),
axis.text = element_text(size = 17),
axis.title = element_text(size = 19,face = "bold"),
legend.title = element_text(size = 17),
legend.text = element_text(size = 15),
plot.title = element_text(lineheight = .8, face = "bold",size = 22))
You get part way there by just doing:
geom_point(size = 3, aes(color = did.it) ...) +
...
scale_color_manual(values = c('red4', 'midnightblue', 'orange', 'black')) ...
But this doesn't leave the points' colours unchanged when did.it is FALSE. So:
d$point_col <- ifelse(d$did.it=='y', 'y', d$Sex)
ggplot(d, aes(x = factor(State), y = Huddling, color = Sex, group = ID, shape = ID))+
facet_grid(Strain ~ ., scales = 'free_y') +
geom_point(size = 3, aes(color = point_col), position = position_dodge(width=0.3),
show.legend = F) +
geom_line(size = 0.7, position = position_dodge(width=0.3)) +
scale_color_manual(values = c('red4', 'midnightblue', 'black')) +
scale_fill_manual(values = "white") +
scale_x_discrete(limits = c("virgin", "mated", "expecting", "parent"),
labels = c("Virgin", "Mated", "Expecting", "Parent")) +
labs(y = "Time huddling (s)", x = "Reproductive stage")
(Plus your extra theme statements.)
I have the plot below and I would like the facet plot labels to be what is in "lbl" =
> lbl
[1] "0% - 10%" "10% - 20%"
How can labeller be added to the facet_wrap to get that text to show up and how does labeller correctly handle the ordering of that is output from the labeller function? i.e. If I have 20 plots how does labeller correctly label the plots in the right order? Thank you.
here is the code:
x = c( rep(c(1,2,3,4,5),4) )
group = c( rep(c(10,10,10,10,10),2),rep(c(20,20,20,20,20),2) )
lbl = paste0( c("0%", paste0( unique(group)[1:(length(unique(group))-1)] ,"%" ) )
," - ",
paste0(unique(group),"%"))
lbl
value = rnorm(20)
dat = data.frame( x= x , group = group, value = value)
dat = dat %>% # create the mu, sd, q1 and q3 aggregates
group_by(group,x) %>%
summarise(mu = round(mean(value),2),
sd= sqrt(round(sd(value),2)),
Q1 = quantile(value)[2],
Q3 = quantile(value)[4],
count = n())
dat
dat2 = dat %>% gather (key = Metric, value= Value,c(mu, sd, Q1, Q3)) #melt the data
as.data.frame(dat2)
ggplot(data=dat2 , aes(x=x, y=Value, group = Metric,colour = Metric,linetype = Metric)) +
geom_line() + geom_point() + ylab("value") +
xlab("v") +
scale_x_discrete(breaks = c( seq(1,5,1) ) ) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_y_continuous(breaks = c( seq(-3,3,.25) ) ) +
scale_colour_manual(values=c(mu = "blue", sd = "blue", Q1 = "red", Q3 = "red")) +
scale_linetype_manual(values =c(mu = "dashed", sd= "solid", Q1 = "solid", Q3 = "solid")) +
facet_wrap(~ group, scales = "free",ncol=3) +
theme(strip.text.x = element_text(size=10, angle=0),
strip.text.y = element_text(size=12, face="bold"),
strip.background = element_rect(colour="red", fill="#CCCCFF"))
You just need to build a labeller; read ?labeller and here, ?as_labeller for help. All you really need to add is labeller = as_labeller(setNames(lbl, sort(unique(group)))) (or a suitably named vector, constructed how you like) to facet_wrap:
ggplot(data=dat2 , aes(x=x, y=Value, group = Metric,colour = Metric,linetype = Metric)) +
geom_line() + geom_point() + ylab("value") +
xlab("v") +
scale_x_discrete(breaks = c( seq(1,5,1) ) ) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_y_continuous(breaks = c( seq(-3,3,.25) ) ) +
scale_colour_manual(values=c(mu = "blue", sd = "blue", Q1 = "red", Q3 = "red")) +
scale_linetype_manual(values =c(mu = "dashed", sd= "solid", Q1 = "solid", Q3 = "solid")) +
facet_wrap(~ group, scales = "free",ncol=3,
# add a labeller here:
labeller = as_labeller(setNames(lbl, sort(unique(group))))) +
theme(strip.text.x = element_text(size=10, angle=0),
strip.text.y = element_text(size=12, face="bold"),
strip.background = element_rect(colour="red", fill="#CCCCFF"))
Just trying to fix this overlapped labeling:
My code:
values=c(164241,179670)
labels=c("Private", "Public")
colors=c("#cccccc", "#aaaaaa")
categoriesName="Access"
percent_str <- paste(round(graph$values / sum(graph$values) * 100,1), "%", sep="")
values <- data.frame(val = graph$values, Type = graph$labels, percent=percent_str )
pie <- ggplot(values, aes(x = "", y = val, fill = Type)) + geom_bar(width = 1) +
geom_text(aes(y = **val + 1**, **hjust=0.5**, **vjust=-0.5**, label = percent), colour="#333333", face="bold", size=10) +
coord_polar(theta = "y") + ylab(NULL) + xlab(NULL) +
scale_fill_manual(values = graph$colors) + labs(fill = graph$categoriesName) +
opts( title = graph$title,
axis.text.x = NULL,
plot.margin = unit(c(0,0,0,0), "lines"),
plot.title = theme_text(face="bold", size=14),
panel.background = theme_rect(fill = "white", colour = NA) )
print(pie)
Tried messing with the values marked with asterisks (** **) but haven't got anywhere.
Any help appreciated.
here is an example:
pie <- ggplot(values, aes(x = "", y = val, fill = Type)) +
geom_bar(width = 1) +
geom_text(aes(y = val/2 + c(0, cumsum(val)[-length(val)]), label = percent), size=10)
pie + coord_polar(theta = "y")
Perhaps this will help you understand how it work:
pie + coord_polar(theta = "y") +
geom_text(aes(y = seq(1, sum(values$val), length = 10), label = letters[1:10]))