As soon as I add a dynamic range for my legend labels within the scale_fill_distiller-option, I get only a "1" instead the defined legend title within labs(fill="title"). What do I have to change?
Here is my code:
map <- ggplot()+
geom_sf(data=data, aes(fill=Count_Number)) +
labs(title="Chart Title",
x="",y="") +
labs(colour = "Legend Title") +
scale_fill_distiller(palette ="RdBu", direction = -1, na.value="#ffffff",
range(c(min(data$Count_Number, na.rm=T),max(data$Count_Number,na.rm=T))),
breaks = seq(min(data$Count_Number, na.rm=T),max(data$Count_Number, na.rm=T),by=2)) +
theme_opts +
theme(plot.title = element_text(size=12, face="bold")) +
theme(legend.position="bottom") +
theme(legend.key.width=unit(2,"cm")) +
theme(legend.title=element_text(size=11)) +
theme(legend.text=element_text(size=12)) +
theme(legend.key.size = unit(0.5, "cm")) +
ggrepel::geom_label_repel(
data = gmddata_covid,
aes(label = Case, geometry = geometry),
stat = "sf_coordinates",
min.segment.length = 0,
color = "#333333",
segment.color = "white",
size = 2.5
)
Thanks in advance for any help
Related
I have the following code :
cols <- c("Bonus"=maroon ,
"Limit"=grey)
a <- ggplot(periodtreatments, aes(x=names, width=.2)) +
geom_point(aes(y=bonus_coefs, colour="Bonus"), stat="identity", position = position_nudge(x = -.1), shape=16) +
geom_point(aes(y=limit_coefs, colour="Limit"), stat="identity", position = position_nudge(x = .1), shape=15)+
geom_errorbar(aes(ymin=bonus_upper, ymax=bonus_lower, width=0.09), size =0.8, stat="identity", colour=maroon, position = position_nudge(x = -.1)) +
geom_errorbar(aes(ymin=limit_lower, ymax=limit_upper, width=0.09), size =0.8, stat="identity", colour=grey, position=position_nudge(x = .1)) +
scale_y_continuous(name="Treatment effect (minutes/day)") +
theme_classic() +
#theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(x = "") +
theme(legend.text.align = 0,
legend.key.height = unit(1, "cm"),
legend.position="bottom") +
theme(legend.margin=margin(0,0,0,0),
legend.box.margin=margin(-10,-10,-10,-10)) +
theme(axis.text.x = element_text(colour="black")) +
coord_cartesian(ylim = c(-70, 5)) +
theme(legend.text=element_text(size=11)) +
theme( # remove the vertical grid lines
panel.grid.major.x = element_blank() ,
# explicitly set the horizontal lines (or they will disappear too)
panel.grid.major.y = element_line( size=.05, color="grey" )
)+
scale_colour_manual(name = "", values=cols,
labels = c("Bonus", "Limit"))+
scale_shape_manual(name = "",
labels = c("Bonus", "Limit"),
values = c(16, 15))
But the "scale_shape_manual" legend does not work.
In fact here is what my plot look like
Why does the Bonus show as a sqaure whereas its shape is 16 = point ???
Please help me i am so helpless
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 am not able to increase the font size of the names of the variables in a graphic realized with ggplot.
I tried to include these codes inside ggplot code, but unsuccessfully :
theme(text = element_text(size=20))
theme(axis.text=element_text(size=20))
theme(axis.title=element_text(size=14))
theme_grey(base_size = 20)
geom_text(size=20)
My code is :
library(ggplot2)
library(reshape2)
dataplot <- read.csv("/Documents/R.csv",header=T,sep=";")
dataPlotMelt <- melt(data = dataplot, id.vars = c("variable"),variable.name = "Method",value.name = "SMD")
varNames <- as.character(dataplot$variable)
dataPlotMelt$variable <- factor(dataPlotMelt$variable,levels = varNames)
ggplot(data=dataPlotMelt,mapping=aes(x=variable,y=SMD,group=Method, color=Method))+
ylab("Standardizedmeandifference(%)")+
xlab("") +
geom_point(aes(shape=Method),size=2) +
geom_hline(yintercept=15,color="black",size=0.1,linetype="dashed") +
geom_hline(yintercept=-15,color="black",size=0.1,linetype="dashed") +
coord_flip() +
theme(axis.text.x=element_blank()) +
scale_y_continuous(breaks=c(-65,-15,15,105)) +
theme_bw() +
theme(legend.text=element_text(size=12)) +
theme(legend.title=element_blank(),legend.key=element_blank()) +
scale_colour_manual(values=c("grey","black"))
I'd like to increase the font size of the names of the variables in the graphic and, besides, increase the text "Standardized mean difference (%)" and remove the vertical line between the yintercept and ybreak on both sides
new graphic
Thank you Richard for giving me the solution.
As you suggested I used theme after theme_bw
I managed to suppress the useless vertical lines as well with the command theme(panel.grid.minor = element_blank())
Here is the new code for ggplot :
ggplot(data = dataPlotMelt, mapping = aes(x = variable, y = SMD,group = Method,
color = Method)) +
ylab("Standardized mean difference (%)") + xlab("") +
geom_point(aes(shape = Method),size=2) +
geom_hline(yintercept = 15, color = "black", size = 0.1, linetype = "dashed") +
geom_hline(yintercept = -15, color = "black", size = 0.1, linetype = "dashed") +
coord_flip() +
theme(axis.text.x = element_blank()) +
scale_y_continuous(breaks=c(-65,-15,0,15,105)) +
theme_bw() + theme(legend.text = element_text(size=13)) +
scale_colour_manual(values= c("grey","black")) +
theme(axis.text.y = element_text(size=12)) +
theme(axis.title.x = element_text(size=13)) +
theme(panel.grid.minor = element_blank()) +
theme(legend.title = element_blank(), legend.key=element_blank())
I would like to know if it's possible to modify the ticks of x axis with a ggplot pie chart.
Here what I can do :
# Some colors
couleurs <- data.frame(
id=seq(1,17,1),
mix=c(c(rep(1,6),rep(2,7),rep(3,4))),
html=c("#A00020","#109618","#388EE4","#C484D1","#FFAA33","#CCCDD0","#004AC5","#F80094","#CB5023","#638995","#33CFCF","#95DC4E","#F7D633","#5C403C","#F72020","#00D96C","#FDE4C5")
)
couleurs$html <- factor(couleurs$html, levels = couleurs$html[order(couleurs$id, decreasing = FALSE)])
# Data
activite <- data.frame(label=c("B to B","B to C","B to B / B to C", "B to B"), cible=c(rep("Externe",3), "Interne"), nb=c(12,9,3,12))
activite$label <- factor(activite$label, levels = activite$label[order(activite$nb[activite$cible=="Externe"], decreasing = TRUE)])
library(plyr)
activite<-ddply(activite,.(cible),transform,pc=(nb/sum(nb))*100)
activite
# Pie chart
library(ggplot2)
ggplot(data = activite, aes(x = "", y = nb, fill = label )) +
geom_bar(stat = "identity", position = position_fill(), width = 1) +
coord_polar(theta= "y", start = 0, direction = -1) +
labs(fill="") +
scale_fill_manual(values=as.character(couleurs$html[1:nrow(activite)]), labels=paste(activite$label,"\t\t\t",sep="")) +
geom_text(aes(label = paste(pc,"%", sep=" ")), size=4, colour = "white", fontface = "bold", position = position_fill(vjust = 0.5)) +
theme(strip.text = element_text(size=20, face = "bold", ), strip.background = element_rect(fill="grey75")) +
theme(panel.background = element_rect(fill = "white")) +
theme(plot.background = element_rect(fill = "grey92")) +
theme(legend.position="bottom", legend.background = element_rect(fill="grey92")) +
theme(legend.key = element_blank()) +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_line(colour = "grey75")) +
theme(axis.text.y = element_blank()) +
theme(axis.ticks.length = unit(0, "mm")) +
theme(axis.title.x = element_blank(),axis.title.y = element_blank()) +
theme(legend.box.spacing = unit(1, "mm")) +
facet_wrap(~ cible)
Here my result:
After several hours of serach, I didn't find a solution to create what I want. The exact same pie chart but with personalised ticks like that :
With these particular conditions :
- do not change the direction of the data in the pie chart, I want it like exactly this
- if possible (but if not possible, it's okay), I would like the ticks' labels not superposed with the axis.
If someone can help me, I would really appreciate.
Here's one solution:
ggplot(data = activite %>%
group_by(cible) %>%
arrange(desc(nb)) %>%
mutate(axis.label = cumsum(nb),
axis.position = cumsum(pc)/100) %>%
mutate(axis.label = ifelse(pc == min(pc),
paste(axis.label, "0", sep = "-"),
axis.label)),
aes(x = 1, y = nb, fill = label )) +
geom_segment(aes(x = 1, xend = 1.6, y = axis.position, yend = axis.position),
colour = "grey75") +
geom_vline(xintercept = 1.6, colour = "grey75") +
geom_bar(stat = "identity", position = position_fill(reverse = T), width = 1) +
coord_polar(theta= "y", start = 0, direction = 1) +
labs(fill="") +
scale_fill_manual(values=as.character(couleurs$html[1:nrow(activite)]), labels=paste(activite$label,"\t\t\t",sep="")) +
geom_text(aes(label = paste(pc,"%", sep=" ")), size=4, colour = "white",
fontface = "bold", position = position_fill(vjust = 0.5, reverse = T)) +
geom_text(aes(x = 1.7, label = axis.label), size = 3,
position = position_fill(reverse = T)) +
theme(strip.text = element_text(size=20, face = "bold", ), strip.background = element_rect(fill="grey75")) +
theme(panel.background = element_rect(fill = "white")) +
theme(plot.background = element_rect(fill = "grey92")) +
theme(legend.position="bottom", legend.background = element_rect(fill="grey92")) +
theme(legend.key = element_blank()) +
theme(panel.grid = element_blank()) +
theme(axis.text = element_blank()) +
theme(axis.ticks = element_blank()) +
theme(axis.title = element_blank()) +
theme(legend.box.spacing = unit(1, "mm")) +
facet_wrap(~ cible)
Explanation:
The sequence in your labels went clockwise, while the direction of the polar coordinates went counter-clockwise. That makes labelling rather troublesome. I switched the direction for polar coordinates, & added reverse = T inside the position adjustment calls for the geoms.
It's hard to assign different axis breaks to different facets of the same plot, so I didn't. Instead, I modified the data to include calculated axis labels / margin positions, added margins via geom_segment / geom_vline, & hid the axis text / ticks in theme().
I have a boxplot with same color boxes. I would like to name the axis with labs(), and add a small symbol in the color of the plot's boxes.
sector <- c("HY", "HY (ETFs)", "IG", "IG (ETFs)", "Loan", "Equities")
YTD_percent <- c(-0.2, 0.5, 0.05, 0.2, -0.1, 0.1)
data1 <- data.frame(sector, YTD_percent)
ggplot(data1, aes(sector, YTD_percent)) +
geom_bar(stat="identity", color="#00669C", fill="#00669C", width=0.7) +
labs(x = NULL, y = NULL, title = "Plot Title") +
coord_flip() +
theme_bw() +
theme(axis.text.y=element_blank(),
axis.ticks.y = element_blank()
)+
guides(fill = guide_legend(keywidth = 1, keyheight = 1)) +
scale_y_continuous(breaks=pretty_breaks(), expand = c(.05, .05), labels=percent) +
geom_text(aes(label = sector , y = YTD_percent), hjust = ifelse(data_ff$YTD_percent >= 0, -0.1, 1.1), size=6)
This is what the axis should look like in the end:
You could include fill in your aesthetics mapping and tune the legend a little bit:
library(ggplot2)
library(scales)
ggplot(data1, aes(sector, YTD_percent, fill = "YTD (in %)")) +
geom_bar(stat="identity", color="#00669C", width=0.7) +
scale_fill_manual(values = c("YTD (in %)" = "#00669C")) +
labs(x = NULL, y = NULL, title = "Plot Title", fill = "") +
coord_flip() +
theme_bw() +
theme(axis.text.y=element_blank(),
axis.ticks.y = element_blank(),
legend.position = "bottom"
)+
guides(fill = guide_legend(keywidth = 1, keyheight = 1)) +
scale_y_continuous(breaks=pretty_breaks(), expand = c(.05, .05), labels=percent) +
geom_text(aes(label = sector , y = YTD_percent), hjust = ifelse(data1$YTD_percent >= 0, -0.1, 1.1), size=6)