I have a complicated R ggplot problem, where I would like to change the linesize for individual datapoints that connect across 4 bar graphs.
With the following data
library(reshape2);library(lemon)
df <- iris
df$linesize <- ifelse(df$Sepal.Length >= 5,0.5,0.1) # make linesize bigger for large sepal length flowers
df$id <- seq(1,nrow(df))
df <- reshape2::melt(df,id.vars=c("Species","id","linesize"))
df <- subset(df,df$id %in% c(seq(1,10),seq(51,61),seq(101,111)))
pj <- position_jitterdodge(seed=9, jitter.height = 0, dodge.width = 0.00)
df_means <- summarySE(df,measurevar = "value","variable",na.rm = TRUE)
with this plot
ggplot(df,aes(x=variable, y=value,fill=variable)) +
geom_bar(data=df_means,stat='identity',width=0.7,position=position_dodge2(width = 0.1, preserve = "single", padding = -0.5)) +
geom_errorbar(data=df_means,aes(ymin=value-se, ymax=value+se), width=.3,size=2) +
lemon::geom_pointline(aes(colour=Species, group=interaction(id,Species)),linesize=df$linesize,size=3,position = pj)
I am able to depict a different color for Species, and with different color for variable.
Although What I would like is that the linesize differs according to the variable in df$linesize (i.e. larger line sizes for those flowers with > 5 sepal length)
moreover, in the below plot, the lines don't fully connect with the dots.
With this second approach
ggplot(df,aes(x=variable, y=value,fill=variable)) +
geom_bar(data=df_means,stat='identity',width=0.7,position=position_dodge2(width = 0.1, preserve = "single", padding = -0.5)) +
geom_errorbar(data=df_means,aes(ymin=value-se, ymax=value+se), width=.3,size=2) +
geom_line(data=df,size=df$linesize,aes(group=id), position = position_dodge(0.2)) +
geom_point(aes(fill=Species,group=id),size=2,shape=21, position = position_dodge(0.2))
I am able to link the line up with the point, but now the Species factor is bundled together with the variable factor in the legend?. Moreover I am still unable to change the linesize.
I would like to manipulate the line size for individual data points, and connect the line fully with the points, while keeping all factors: Species / Variable / Linesize separate in the legend.
Thanks for reading

