Changing line types in density plot

905 views
Skip to first unread message

gafna jeff

unread,
Sep 6, 2021, 7:25:24 AM9/6/21
to ggplot2
Hi everyone,
I would really appreciate your help on this.
How can I change the line types in this example density plot. For instance dashed, dotted lines etc. In this example, I am using the diamonds data available in R base. I tried adding this line but it does not change the line type:     scale_linetype_manual(values = c("dashed", "solid", "dashed", "solid"))


#Here is the example 
library(ggplot2)

data("diamonds")
View(diamonds)

ggplot(diamonds, aes(depth, colour = cut)) + scale_linetype_manual(values = c("dashed", "solid", "dashed", "solid")) +
  geom_density()




Tom Fritzsche

unread,
Sep 6, 2021, 7:39:20 AM9/6/21
to gafna jeff, ggplot2
Hi Gafna Jeff,

In order to scale something manually you need to mention it in the aesthetics.
Assuming that you would like to vary line types by the factor 'cut'
the code below should work.
Since there are five factor levels, I added one more linetype
('dashed') in the manual command.

ggplot(diamonds, aes(depth, colour = cut, lty = cut)) +
scale_linetype_manual(values = c("dashed", "solid", "dashed", "solid",
"dashed")) +
geom_density()

Hope this helps,
Tom

---
Tom Fritzsche
University of Potsdam
Department of Linguistics
Karl-Liebknecht-Str. 24-25
14476 Potsdam
Germany

office: 14.140
phone: +49 331 977 2296
fax: +49 331 977 2095
email: tom.fr...@uni-potsdam.de
web: www.ling.uni-potsdam.de/~fritzsche
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ggplot2/CAABPRJen2BHzJrfB0jYCQtnZ53trciqJdvaO7yZBPi1CfCp2Ow%40mail.gmail.com.

gafna jeff

unread,
Sep 6, 2021, 7:42:20 AM9/6/21
to Tom Fritzsche, ggplot2
Dear Tom,

Thanks for your help. It works perfectly. Much appreciated

Thierry Onkelinx

unread,
Sep 6, 2021, 8:31:42 AM9/6/21
to gafna jeff, ggplot2
You need to add an aesthetic for the linetype too.

ggplot(diamonds, aes(depth, colour = cut, linetype = cut))  + geom_density()


ir. Thierry Onkelinx
Statisticus / Statistician

Vlaamse Overheid / Government of Flanders
INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND FOREST
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
thierry....@inbo.be
Havenlaan 88 bus 73, 1000 Brussel
www.inbo.be

///////////////////////////////////////////////////////////////////////////////////////////
To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey
///////////////////////////////////////////////////////////////////////////////////////////





Op ma 6 sep. 2021 om 13:25 schreef gafna jeff <gafn...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages