After referring to multiple links i have got to the below code however i still am not succeeding to get the line with labels. I suspect some mistake in sec.axis transformation but i can't figure it out.
# dummy data
df_dummy = data.frame('Plan_code'=c('A','B','C','D','E','F','G'),
'Total'=c(191432,180241,99164,58443,56616,29579,19510),'STP'=c(41,40,44,37,37,37,45))
# creation of plot
[![g <- ggplot(data = df_dummy, aes(x = Plan_code, y = Total)) +
geom_col(aes(fill = 'Total')) +
geom_line(data = df_dummy, aes(x = Plan_code, y = STP,group=1)) +
geom_point(data = df_dummy, aes(x = Plan_code,y=STP)) +
geom_label(data = df_dummy, aes(x = Plan_code, y = STP, fill = Plan_code, label = paste0('%', STP)), color = 'white', vjust = 1.6, size = 3) +
scale_y_continuous(sec.axis = sec_axis(~. / 2000, name = 'PERCENT')) +
labs(fill = NULL, color = NULL) +
theme_minimal()
print(g)][1]][1]
Like that?
g <- ggplot(data = df_dummy, aes(x = Plan_code, y = Total)) +
geom_col(aes(fill = 'Total')) +
geom_point(data = df_dummy, aes(x = Plan_code,y=STP * 2000)) +
geom_label(data = df_dummy, aes(x = Plan_code, y = STP *2000, fill = Plan_code, label = paste0('%', STP)), color = 'white', vjust = 1.6, size = 3) +
scale_y_continuous(sec.axis = sec_axis(~. / 2000, name = 'PERCENT'))+
geom_line(data = df_dummy, aes(x = Plan_code, y = STP * 2000,group=1), col = 'blue') +
theme(axis.text.y.right = element_text(color = 'blue'),axis.title.y.right = element_text(color = 'blue'))
labs(fill = NULL, color = NULL) +
theme_minimal()
I just multiplied your data with 2000, so that the absolute y-coordinates were right.
And I changed the color.
Related
I have a plot that includes data from two different scales. So far, I've plotted both variables and adjusted the scale of one variable (ss) so that it is closer to the other variables. This greatly reduced the white space in the middle of the plot.
set.seed = 42
df <- data.frame(
cat = runif(10, 1, 20),
mean = runif(10, 350, 450),
ss = runif(10, 1, 50))
ggplot(data = df) +
geom_bar(aes(x = cat, y = ss + 250),
stat = "identity",
fill = "red") +
geom_point(aes(x = cat, y = mean)) +
geom_smooth(aes(x = cat, y = mean),
method = "loess", se = TRUE) +
scale_y_continuous(sec.axis = sec_axis(trans = ~.-250,
name = "sample size")) +
labs(y = "mean") +
theme_bw()
However, I don't love the really long bars for sample size, and I'd like to change the limits on the left y axis so that it starts 250 (where ss = 0). Unfortunately, if I replace my current scale_y_continuous parameter with limits (see below), then the bars disappear. How do I do this?
ggplot(data = df) +
geom_bar(aes(x = cat, y = ss + 250),
stat = "identity",
fill = "red") +
geom_point(aes(x = cat, y = mean)) +
geom_smooth(aes(x = cat, y = mean),
method = "loess", se = TRUE) +
scale_y_continuous(limits = c(250, 510), ### NEW Y AXIS LIMITS
sec.axis = sec_axis(trans = ~.-250,
name = "sample size")) +
labs(y = "mean") +
theme_bw()
EDIT: Updated plot with #AllanCameron's suggestion. This is really close, but it has the values of the bars extend below 0 on the secondary axis.
ggplot(data = df) +
geom_bar(aes(x = cat, y = ss + 250),
stat = "identity",
fill = "red") +
geom_point(aes(x = cat, y = mean)) +
geom_smooth(aes(x = cat, y = mean),
method = "loess", se = TRUE) +
scale_y_continuous(sec.axis = sec_axis(trans = ~.-250,
name = "sample size")) +
labs(y = "mean") +
theme_bw() +
coord_cartesian(ylim = c(250, 510)) ### NEW
Just expand parameter in scale_y_continuous() to c(0,0).
This tells ggplot2 to not add padding to the plot box.
ggplot(data = df) +
geom_bar(aes(x = cat, y = ss + 250),
stat = "identity",
fill = "red") +
geom_point(aes(x = cat, y = mean)) +
geom_smooth(aes(x = cat, y = mean),
method = "loess", se = TRUE) +
scale_y_continuous(sec.axis = sec_axis(trans = ~.-250, name = "sample size"),
expand = c(0,0)) + # New line here!
labs(y = "mean") +
theme_bw() +
coord_cartesian(ylim = c(250, 510))
i would like to have help with my graph. I would like to be able to change the graph representation algorithm (LGL or other) but I can't. How can I do it? have tried several options that do not work...
library(NetworkToolbox)
library(dplyr)
library(igraph)
library(ggplot2)
library(ggnetwork)
M1 <- as_tibble(replicate(21,sample(1:3,100,rep=TRUE)))
colnames(M1) <- c("1st", "2nd", "3th", "4th", "5th", "6th","7th","8th","9th","10th",
"11th","12th","13th","14th","15th","16th","17th","18th","19th",
"20th","21th")
M2 <- as.matrix(round(cor(M1[,],method ="kendall"),2))
gr4ph <- graph.adjacency(M2, mode = "undirected",weight=TRUE)
MAST <- MaST(M2, normal = False)
gr4ph <- graph.adjacency(MAST , mode = "lower",weight=TRUE)
ggplot(gr4ph, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(color = "grey", alpha = 1) +
geom_nodes(aes(color = name)) + theme_blank() +
geom_nodetext(aes(label = name), color = "black") +
geom_edgetext(aes(label = weight))+
theme(legend.position = "none")
You can use two solution. The layout argument is the key of the graph's representation :
The following exemple are coded with "Large graph layout"
With ggplot :
ggplot(gr4ph1, aes(x = x, y = y, xend = xend, yend = yend),layout = layout_with_lgl(gr4ph1))+
geom_edges(color = "grey", alpha = 1,size=1.5,) +
geom_nodes(aes(color = name), size = 3) + theme_blank() +
geom_nodetext(aes(label = name), color = "black", size = 3) +
geom_edgetext(aes(label = weight), size = 3,label.padding=unit(0.01, "lines"))+
theme(legend.position = "none")
With ggraph :
ggraph(gr4ph, layout = "lgl") +
geom_edge_link(aes(label = weight), angle_calc = 'along',label_dodge = unit(2.5, 'mm')) +
geom_node_point(aes(size=3,color= name)) +
theme(legend.position = "none")+
geom_node_text(aes(label = name), repel = TRUE)
This question already has answers here:
How to add legend to ggplot manually? - R [duplicate]
(1 answer)
Missing legend with ggplot2 and geom_line
(1 answer)
Closed 1 year ago.
I'm trying to plot some poll results but always have to add the legend manually after creating the plot. Here's my data (poll.csv):
Date,AKP,MHP,CHP,IYI,HDP,DEVA,GP,SP
01-05-2021,34.8,9,27.2,13.1,8.9,3.1,1.8,1.6
01-06-2021,36.2,9.1,20.3,16.7,10.5,3.6,1.3,0.6
01-14-2021,35,9.5,25,13,10.7,2.5,2.6,0.8
01-27-2021,35.6,8.5,24.9,13.6,8.9,2.3,2.5,1.6
01-30-2021,35.6,8.1,25.3,13.4,10.5,2.9,3,0.9
02-06-2021,35.8,7.8,24.2,12.7,11.4,4.6,1.3,0.8
02-08-2021,39.3,10.5,22.6,13.1,10.2,,,1.2
02-13-2021,35.2,9.6,21.1,15.6,10.6,3.8,1.7,1
02-13-2021,34.1,7.5,27.3,14,10.1,2.4,2.7,1.4
02-15-2021,35.2,9,26.5,12.9,9.9,2.5,1,1.5
02-23-2021,36.8,11.6,22.3,10.8,8.2,2.1,2.6,1.3
02-23-2021,41.7,11.6,21.8,10.3,9.3,1.8,0.4,0.8
02-26-2021,36.1,10,24.1,14,10.1,1.6,1.4,1.3
03-03-2021,34.8,7,26.2,13.1,9.1,2.5,2.7,0.7
03-06-2021,36.5,8.1,24,13.1,11,4,,
03-08-2021,36,9.9,26.3,13.5,8.7,2.1,1.9,1.1
03-10-2021,36,,29,,10.4,,,
03-11-2021,35.4,8.3,22,16.5,11.3,2.9,1.2,1.2
03-14-2021,34.4,7.2,26.2,13.9,9.6,2.7,2.8,0.8
and here's my code:
library(ggplot2)
library(anytime)
poll = read.csv("poll.csv")
poll$Date = as.Date(anydate(poll$Date))
ggplot(poll) +
geom_point(aes(x = Date, y = AKP), color = '#ff8700') +
geom_smooth(aes(x = Date, y = AKP), color = '#ff8700') +
geom_point(aes(x = Date, y = CHP), color = '#ff0000') +
geom_smooth(aes(x = Date, y = CHP), color = '#ff0000') +
geom_point(aes(x = Date, y = MHP), color = '#ff0019') +
geom_smooth(aes(x = Date, y = MHP), color = '#ff0019', se = FALSE) +
geom_point(aes(x = Date, y = HDP), color = '#8000ff') +
geom_smooth(aes(x = Date, y = HDP), color = '#8000ff', se = FALSE) +
geom_point(aes(x = Date, y = IYI), color = '#3db5e6') +
geom_smooth(aes(x = Date, y = IYI), color = '#3db5e6', se = FALSE) +
geom_point(aes(x = Date, y = SP), color = '#f50002') +
geom_smooth(aes(x = Date, y = SP), color = '#f50002', se = FALSE) +
geom_point(aes(x = Date, y = DEVA), color = '#0061a1') +
geom_smooth(aes(x = Date, y = DEVA), color = '#0061a1', se = FALSE) +
geom_point(aes(x = Date, y = GP), color = '#00564a') +
geom_smooth(aes(x = Date, y = GP), color = '#00564a', se = FALSE) +
scale_x_date(date_breaks = "3 months", date_labels = "%m/%y") +
labs(x = "", y = "") +
theme(axis.text.x = element_text(size = 20),
axis.text.y = element_text(size = 20))
I searched for answers but couldn't make any one of them work. How can I add a legend to the plot?
I have two plots I just want to know how I can add a legend for the blue and gray bar charts and also could you please show me how you could also edit the legend tittle.
X1 <- c(seq(7.912087912,44.83516484,1.538461538))
X2 <- c(seq(7.912087912,49.45054945,1.538461538))
dat2 <- data.frame(x = X2 , y = rnorm(28, 26, 5))
dat1 <- data.frame(x = X1 , y = rnorm(100, 25, 4))
ggplot(NULL) +
geom_bar(dat1, mapping = aes(x = x, y = y), stat = "identity",alpha = 0.3, position = "stack" ) + labs( x = " Time [ S ]", y = "Frequency") + theme_minimal() +
ggtitle("Histogram Of Time In Tank") + theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(hjust = 0.5)) +
geom_bar(dat2, mapping = aes(x = x, y = y ), stat = "identity", alpha = .3, position = "stack", fill='lightblue' , color='lightblue4')
+ scale_linetype_discrete(name = LegendTitle)
If you want a legend in ggplot, you need to have an aesthetic mapping inside your aes() or no legend will appear. Here's how we can set a mapping and then use the scale to set the colors we want
ggplot(NULL) +
geom_bar(dat1, mapping = aes(x = x, y = y, fill="Grey Bars"), stat = "identity",alpha = 0.3, position = "stack" ) +
labs( x = " Time [ S ]", y = "Frequency") +
theme_minimal() +
ggtitle("Histogram Of Time In Tank") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_bar(dat2, mapping = aes(x = x, y = y, fill='Blue Bars') , stat = "identity", alpha = .3, position = "stack", color='lightblue4') +
scale_fill_manual(name="Bars", values=c("Grey Bars" = "grey35", "Blue Bars" = "lightblue"))
I have a serie of plots (more than 10) arranged on 2 colonnes using grid.arrange. Since they all possess the same x axis labels, I am ploting the x axis labels only for the last array. However, the plots part of the last array are compressed and reduced in size
Here is a reproducible example of the problem:
source("http://www.openintro.org/stat/data/arbuthnot.R")
library(ggplot2)
library(reshape2)
library(gridExtra)
names(arbuthnot) <- c("Year", "Men", "Women")
arbuthnot.melt <- melt(arbuthnot, id.vars = 'Year', variable.name = 'Sex',
value.name = 'Rate')
p1 <- ggplot(arbuthnot.melt, aes(x = Year, y = Rate, shape = Sex, color = Sex, linetype = Sex))+
geom_line() + scale_color_manual(values = c("Women" = '#ff00ff','Men' = '#3399ff')) +
scale_linetype_manual(values = c('Women' = 'solid', 'Men' = 'dotted')) +
theme (axis.text.x=element_blank(),axis.title.x=element_blank())
p2 <- ggplot(arbuthnot.melt, aes(x = Year, y = Rate, shape = Sex, color = Sex, linetype = Sex))+
geom_line() + scale_color_manual(values = c("Women" = '#ff00ff','Men' = '#3399ff')) +
scale_linetype_manual(values = c('Women' = 'solid', 'Men' = 'dotted')) +
theme (axis.text.x=element_blank(),axis.title.x=element_blank())
p3 <- ggplot(arbuthnot.melt, aes(x = Year, y = Rate, shape = Sex, color = Sex, linetype = Sex))+
geom_line() + scale_color_manual(values = c("Women" = '#ff00ff','Men' = '#3399ff')) +
scale_linetype_manual(values = c('Women' = 'solid', 'Men' = 'dotted')) +
scale_x_continuous(expand = c(0, 0), labels=c("FSSFDFSDSDF", "ASD","QDDZXCC","QDDZXCC","QDDZXCC")) +
theme(axis.text.x = element_text(hjust = 1, angle=90))
p4 <- ggplot(arbuthnot.melt, aes(x = Year, y = Rate, shape = Sex, color = Sex, linetype = Sex))+
geom_line() + scale_color_manual(values = c("Women" = '#ff00ff','Men' = '#3399ff')) +
scale_linetype_manual(values = c('Women' = 'solid', 'Men' = 'dotted')) +
scale_x_continuous(expand = c(0, 0), labels=c("FSSFDFSDSDF", "ASD","QDDZXCC","QDDZXCC","QDDZXCC")) +
theme(axis.text.x = element_text(hjust = 1, angle=90))
grid.arrange(p1, p2, p3, p4, ncol=2)
Is it possible to standardize the plot formats with no regards to the fact that they possess or not a legend? Im my case, it is even impossible to read the content of the last two plots...