i have run the following code yet what is inside the "theme" function doesn't work (it does not change the legend title nor the axis title). Could anyone help fix this problem ?
ggplot() +
geom_line(aes(x = df$année,
y = df$nb_amb,
color = df$Type_amb,
group = 1)) +
geom_line(aes(x = df$année,
y = df$nb_hosp,
color = df$type_hosp,
group = 1)) +
geom_line(aes(x = df$année,
y = df$nb_ext,
color = df$type_ext,
group = 1)) +
theme(axis.title.y = element_text("trip"),
axis.title.x = element_text("year"),
legend.title = element_text("alpha"))
Putting the comments together, your code would look something like this:
ggplot(PGS_evol_type) +
geom_line(aes(x = année, y = nb_amb, color = type_amb, group = 1)) +
geom_line(aes(x = année, y = nb_hosp, color = type_hosp, group = 1)) +
geom_line(aes(x = année, y = nb_ext, color = type_ext, group = 1)) +
labs(y = "nombre de séjour", x = "année", color = "type de séjour") +
# Theme is used for changing the look of objects, for example
theme(
axis.title.x = element_text(face = "bold", size = "14"),
axis.title.y = element_text(face = "italic", size = "12")
)
Related
I have horizontal dots plot replotted via ggplotly
df <- data.frame (origin = c("A","B","C","D","E","F","G","H","I","J"),
Percentage = c(23,16,32,71,3,60,15,21,44,60),
rate = c(10,12,20,200,-25,12,13,90,-105,23),
change = c(10,12,-5,12,6,8,0.5,-2,5,-2)
library(ggplot2)
plt <- ggplot(df, aes(x = rate, y = factor(origin, rev(origin)))) +
geom_hline(aes(yintercept = origin), color = 'gray') +
geom_vline(xintercept = 0, linetype = 2, color = 'gray') +
geom_point(aes(color = 'Rate'), size = 10) +
geom_text(aes(label = rate), color = 'white') +
geom_point(aes(x = change, color = 'Change'), size = 10) +
geom_text(aes(label = change, x = change)) +
theme_minimal(base_size = 16) +
scale_x_continuous(labels = ~paste0(.x, '%'), name = NULL) +
scale_color_manual(values = c('#aac7c4', '#5f9299')) +
theme(panel.grid = element_blank(),
axis.text.y = element_text(color = 'gray50')) +
labs(color = NULL, y = NULL)
ggplotly(plt)
The only issue is that when I hide one of the dot from the figure, texts are still appeared (see below), so is there way to tackle this issue and hide text with circle by clicking on legend?
P.S
(setting text colour in white color = 'white' is not option for me)
You can use the fill aesthetic for the points (as long as they are shape = 21) and use the color aesthetic for the text. As long as these have the same labels for aesthetic mapping, the interactivity for both points and text will be linked.
One minor annoyance is that this changes the plotly legend labels, even though they are correct in the ggplot version. This requires a little direct manipulation of the plotly object itself:
plt <- ggplot(df, aes(x = rate, y = factor(origin, rev(origin)))) +
geom_segment(aes(x = -100, xend = 200,
y = origin, yend = origin), color = 'gray') +
geom_vline(xintercept = 0, linetype = 2, color = 'gray') +
geom_point(aes(fill = 'Rate'), shape = 21, size = 10, color = NA) +
geom_text(aes(label = rate, color = 'Rate')) +
geom_point(aes(x = change, fill = 'Change'),
color = NA, shape = 21, size = 10) +
geom_text(aes(label = change, x = change, color = "Change")) +
theme_minimal(base_size = 16) +
scale_x_continuous(labels = ~paste0(.x, '%'), name = NULL) +
scale_fill_manual(values = c('#aac7c4', '#5f9299')) +
scale_color_manual(values = c("black", "white")) +
theme(panel.grid = element_blank(),
axis.text.y = element_text(color = 'gray50')) +
labs(color = NULL, y = NULL, fill = NULL)
p <- ggplotly(plt)
p$x$data[[3]]$name <- p$x$data[[3]]$legendgroup <-
p$x$data[[4]]$name <- p$x$data[[4]]$legendgroup <- "Rate"
p$x$data[[5]]$name <- p$x$data[[5]]$legendgroup <-
p$x$data[[6]]$name <- p$x$data[[6]]$legendgroup <- "Change"
p
This gives us the following plot:
Now clicking on Rate we get:
And clicking on Change we get
I cannot seem to change the legend title without the legend splitting my shape and color into two separate legends. How can i change the combined legend title? The image is what the graph looks like.
ggplot(data = df, aes (x = factor(dminp,c("-3 to -1", "-1 to 1")), y = sum_diff,col = factor(dmin), shape = factor(dmin), group = factor(dmin)))+
xlab("Range of Difficulty Parameters for Screen Items") + ylab("Bias Due to Skip-Logic") +
stat_summary(geom = "point",fun.y = "mean",size = 8, aes(shape = factor(dmin)))+
stat_summary(geom = "point",fun.y = "mean",size = 8, aes(col = factor(dmin)))+
scale_shape_manual(values = c(8,5)) + theme_bw() + scale_colour_manual(values = c("orange","purple"))+
theme(panel.grid.major.x = element_blank(),
panel.grid.major = element_line(colour = "black",size=0.25))+ theme(legend.justification = "top")
I have tried using labs(col = "what i want it to be named") but this adds a 2nd legend and splits shape/color.
How about trying:
... +
scale_shape_manual(name="X",values = c(8,5)) +
scale_colour_manual(name="X",values = c("orange","purple"))+
..
Here's an example:
ggplot(iris,aes(x=Sepal.Width,y=Sepal.Length,shape=Species,col=Species)) +
geom_point()+
scale_color_manual(name="X",values=c("Blue","Orange","Red")) +
scale_shape_manual(name="X",values=c(17,18,19))
I am trying to make a combo chart using ggplot2. However i want to add a text box sort outside my plot body. I am unable to place it at the desired location
I have used grid pack to create grob and include that in annotation in the ggplot code. Additionally i have also put the same text in geom_text. How do i ensure the text comes say below the legend. Following is my code
m <- ggplot() +
geom_area(data= (ly_vol_ntwk %>%
mutate(Wk_end_d = as.factor(Wk_end_d))%>%
filter(!is.na(value_new))),
aes(x = Wk_end_d, y = value_new ,group = variable,fill=variable))+
geom_bar(data = (fcst_act_vol_ntwk %>%
mutate(Wk_end_d = as.factor(Wk_end_d))%>%
filter(!is.na(value_new))),
aes(x = Wk_end_d, y = value_new, group = variable, fill = variable),
stat = "identity",position = "dodge", width =0.5)+
geom_line(data = (var_vol_ntwk %>%
mutate(Wk_end_d = as.factor(Wk_end_d))%>%
filter(!is.na(value_new))),
aes(x = Wk_end_d, y = value_new,
group = variable, fill= variable), size = 0.8)+
scale_y_continuous(sec.axis = sec_axis(trans = ~./100000,
name = "Variance", breaks = waiver(),
labels=function(x) paste0(x,"%")))+
theme_set(theme_bw())+
theme(axis.text.x = element_text(angle=65, vjust=0.5,face = "plain"),
text = element_text(size=9), legend.position = "bottom", legend.title = element_blank())+
labs(title= "Inbound - Network", x= "Week end date", y = " ")+
scale_fill_manual(values = c("#C5E0B4","#7030A0", "#D9D9D9","#ED7D31","black"))+
geom_text(label = "LW Variance",
aes(x = 19, y = -1960000),
check_overlap = TRUE) #annotation_custom(grob = textGrob("LW Variance"), xmin = 18, xmax = 18, ymin = -1030000, ymax = -1030000)+ coord_cartesian(clip = 'off')
I need to get the text box with a border outside the area of the ggplot. Can you please help me?
You can place text below plot area with labs(caption = "text"), but you can't place captions on top of the plot. However, you could use subtitles labs(subtitle = "text") to produce a similar visual of captions on the top.
To further control the aspect of both options use theme(plot.caption = element_text(...), plot.subtitle = element_text(...)). Type ?element_text in your console to get all the options for text formatting.
For example:
library(ggplot2)
df <- data.frame(x = rnorm(50), y = rnorm(50))
ggplot(df, aes(x, y)) +
geom_point() +
labs(subtitle = "Your text here", caption = "Your text here") +
theme(plot.caption = element_text(colour = "red", hjust = 0, angle = 15),
plot.subtitle = element_text(size = 18, face = "bold", hjust = 0.8))
If you want it below your current legend, you can always add a dummy legend and put your text as its name. An example:
ggplot(mtcars, aes(mpg, wt, color = gear,fill = "a")) +
geom_point() +
scale_fill_discrete(name = "Your custom caption\ngoes here", labels = "") +
theme(legend.key = element_rect(fill = "white")) +
guides(color = guide_legend(order = 1),
fill = guide_legend(order = 2, override.aes = list(linetype = 0, shape=NA))) # setting the order parameter in guide_legend will help place it below your existing legend(s)
I want to build a ggplot graph for a given data.frame with one x-axis and multiple y.curves. Also, I want to do it within a customized function so i could call this function anytime I want to plot something with various dataframes.
The script I'm trying to develop is:
graph.date <- function(data, y.axis1, y.axis2, y.axis3, y.axis4, y.axis5, y.axis6, y.axis7, x.axis, y.lab, title, ...){
ggplot(data, aes_string(x = x.axis)) +
ylab(label = y.lab) + xlab(label = "Date") +
ggtitle(label = title) +
scale_x_date(breaks = "1 month", labels = date_format("%d-%b-%Y")) +
geom_line(aes(y = y.axis1, colour = y.axis1), size = 1) +
geom_line(aes(y = y.axis2, colour = y.axis2), size = 1) +
geom_line(aes(y = y.axis3, colour = y.axis3), size = 1) +
geom_line(aes(y = y.axis4, colour = y.axis4), size = 1) +
geom_line(aes(y = y.axis5, colour = y.axis5), size = 1) +
geom_line(aes(y = y.axis6, colour = y.axis6), size = 1) +
geom_line(aes(y = y.axis7, colour = y.axis7), size = 1) +
scale_fill_discrete() + scale_color_manual(values = c(brewer.pal(9, "Set1"), brewer.pal(9, "Set1"))) +
labs(colour = "") + theme(plot.title = element_text(size = rel(1.76))) +
guides(colour = guide_legend(override.aes = list(size=3))) +
theme(text = element_text(size=20), axis.title=element_text(size=34,face="bold"), axis.text.x = element_text(face="bold",
color="black", size=24, angle=25), axis.text.y = element_text(face="bold", color="black", size=24, angle=0))
}
Then I am calling the function:
graph.date(data = BelgiumMerged, y.axis1 = "Gen1", y.axis2 = "Gen2", y.axis3 = "Gen3",
x.axis = "Date", y.lab = "Capacity", title = "title")
The error I get is :
Error in eval(expr, envir, enclos) : object 'y.axis1' not found
The error you get is that df does not have a column called y.axis1. The easiest way to refer to the column that have the name that is stored in the variable y.axis1 is to use aes_string() instead of aes(). Also don't set the color in the call to aes()
So change all
geom_line(aes(y = y.axis1, colour = y.axis1), size = 1)
to
geom_line(aes_string(y = y.axis1), size = 1,color="red") # Or whatever color you want
However a better way to solve the problem is to reshape the dataframe to long formating so that all x coordinates lays in one column all y coordinates in one column and the grouping of these in a third column. Your function could then be defined as
graph.date <- function(df,y.axes,x.axis){
index <- which(names(df) %in% y.axes)
plotDF <- gather(df,y.type,y.data,index)
ggplot(plotDF,aes_string(x.axis)) +
geom_line(mapping=aes(y=y.data,color=y.type))
}
Here you will pass a vector of y axes instead of having one parameter for each y axis
Thanks a lot Nist - you're a RockStar
My final script looks as follows:
graph.date <- function(data,y.axes,x.axis, y.lab, x.lab, title){
index <- which(names(data) %in% y.axes)
plotDF <- gather(data,y.type,y.data,index)
ggplot(plotDF,aes_string(x.axis)) + ggtitle(label = title) + ylab("Capacity [MW]") + xlab("Date") +
geom_line(mapping=aes(y=y.data,color=y.type))+
scale_fill_discrete() + scale_x_date(breaks = "1 month", labels = date_format("%d-%b-%Y")) +
scale_color_manual(values = c(brewer.pal(9, "Set1"), brewer.pal(9, "Set1"))) +
labs(colour = "LegendTitle") + theme(plot.title = element_text(size = rel(1.76))) +
guides(colour = guide_legend(override.aes = list(size=3))) +
theme(text = element_text(size=20), axis.title=element_text(size=34,face="bold"),
axis.text.x = element_text(face="bold", color="black", size=24, angle=25),
axis.text.y = element_text(face="bold", color="black", size=24, angle=0))
}
#Calling the function
graph.date(df, y.axes = c("Gen1", "Gen2", "Gen3"), x.axis = "Date", title = "title")
How to add legend manually inside the picture. I tried to find an answer on the web but failed to find an appropriate one. Please advice.
Thanks for your help in advance.
p = ggplot(gData, aes(seq, mue)) + geom_line(aes(x = seq, y = mue), lty=1, lwd=1) + xlim(-10, 10) + ylim(-1.0, 1.0) +
xlab(expression(paste(u[t-1],sep = ""))) + ylab(expression(paste(mu[t],""))) +
geom_line(aes(x = seq, y = se1), type="b", lty=2, lwd=1.1) + geom_line(aes(x = seq, y = se2), type="b", lty=2, lwd=1.1) +
theme(axis.title.x = element_text(face = "bold", color = "black", size = 14),
axis.title.y = element_text(face = "bold", color = "black", size = 14))
You can manually choose a legend position using theme(legend.position=c(num, num)). Considering the OP's comment below, I took the following approach.
# Sample data
seq <- rep(c(1:10), times = 3)
value <- rep(c(10,9,6,5,3,2), each = 5)
category <- rep(c("se1", "me", NA), each = 10)
foo <- data.frame(seq, value, category, stringsAsFactors = FALSE)
ggplot(data = foo, aes(x = seq, y = value, color = category)) +
geom_line(lty=1, lwd=1) +
theme(legend.position=c(.8, .5)) +
scale_x_continuous(breaks = seq)