GGPLOT - How to differentiate line according to predefined label and add text in x-axis

ggplot2, plot, r

Solution

To change type of lines you should put `linetype=dat.Function` inside the `aes()` and then use `scale_linetype_manual()` to change line types, similar is with line size - put `size=dat.Function` inside `aes()` and then use `scale_size_manual()` to change line width (you should remove `size=0.5` form `stat_density()`) To mark position of vertical line one possibility is to change breaks on x axis with `scale_x_continuous()` or add some text inside plot with `annotate()`.

ggplot(dat.sub,aes(dat.Freq,color=dat.Function,
            linetype=dat.Function,size=dat.Function))+
  stat_density(geom="path",position="identity")+
  scale_color_manual(values=pal)+
  geom_vline(xintercept=800,colour="red",linetype="longdash")+
  scale_linetype_manual(values=c(2,1,1))+
  scale_size_manual(values=c(2,0.8,0.8))+
  scale_x_continuous(breaks=c(0,800,2500,5000,7500,10000))+
  annotate("text",label="x=800",x=800,y=-Inf,hjust=0,vjust=1)

If you want to place text `x=800` under the axis one way is to use grid objects, other possibility is to play with `scale_x_continuous()` and `theme()`. First, in `scale_x_continuos()` set the `breaks=` and `labels=` and for position 800 use `x=800`. Now there are 6 numbers under the x axis. Using `theme()` and `axis.text.x=` you can change features of texts. If you give vector of values for an element to change then each axis text will have separate feature (I set different feature for the 2. element (text x=800))

ggplot(dat.sub,aes(dat.Freq,color=dat.Function,
               linetype=dat.Function,size=dat.Function))+
  stat_density(geom="path",position="identity")+
  scale_color_manual(values=pal)+
  geom_vline(xintercept=800,colour="red",linetype="longdash")+
  scale_linetype_manual(values=c(2,1,1))+
  scale_size_manual(values=c(2,0.8,0.8))+
  scale_x_continuous(breaks=c(0,800,2500,5000,7500,10000),
                     labels=c(0,"x=800",2500,5000,7500,10000))+
  theme(axis.text.x=
       element_text(color=c("grey35","red","grey35","grey35","grey35"),
                    size=rel(c(1,1.2,1,1,1,1)),
                    face=c("plain","bold","plain","plain","plain","plain")))

Problem

I have a distributions file that looks like this: ``` Function Freq foo 1117 ... qux 1992 .. bar 4158 .. ``` The complete data can be downloaded here. What I want to do is to create density plot, with the following: - Put a geom_vline at x=800 and also show the value 800 in the plot. - For distribution 'bar' use dashed line with different thickness, instead of normal full line. Such that it creates graph like this. But I'm stuck with the following code. What's the best way to do it? ``` library(ggplot2) library(RColorBrewer) pal <- c(brewer.pal(8,"Dark2"),brewer.pal(12,"Paired")); dat<-read.table("http://dpaste.com/1051018/plain/",header=TRUE); dat.sub <- data.frame(dat$Function,dat$Freq) ggplot(dat.sub,aes(dat.Freq,color=dat.Function),shape=dat.Function)+ stat_density(geom="path",position="identity",size=0.5)+ theme(legend.position="none")+ scale_color_manual(values=pal)+ geom_vline(xintercept=800,colour="red",linetype="longdash") ```

Original source