[R] colours in ggplot2

Thierry Onkelinx thierry.onkelinx at inbo.be
Fri Feb 20 14:02:46 CET 2015


Dear Antonello,

You can specify the colours manually with scale_colour_manual(). See
http://docs.ggplot2.org/0.9.3.1/scale_manual.html for some examples. The
last examples uses greys.

Best regards,

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium

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

2015-02-20 13:54 GMT+01:00 Antonello Preti <antoviral op gmail.com>:

> Hi, I'm using ggplot2 to make a plot of the regression of a variable x (let
> say, levels of depression),
> on a variable y (let say, degree of social impairment),
> by taking into account a binary factor (having had or not a past admission
> to a psychiatric service),
> and age of partecipants.
>
> After some search in Internet I produced a code which is satisfying to me.
> This site was very helpful: http://editerna.free.fr/wp/?p=266
>
> However, I have a problem: no matter what I try, the figures always include
> bluette and pink flamingo colours.
> The figure is for an academic article, and I cannot afford the price of
> having the plot printed in colours.
>
> I've extracted the structure of the figure, and I understand that the
> problem is in the scale_name "hue",
> but I cannot figure out how to deal with it.
>
> Any way to override the ggplot2 system of dealing with factors?
>
> Here the codes and the sessionInfo
> The code is a bit baroque, but this is the best I was able to do.
>
>
> Thank you in advance,
> Antonello Preti
>
>
>
> ######## code for exemplification
>
> ### the dataset
>
> df <- structure(list(Social_impairment = c(2.83, 3.08, 2.75, 2.08,
> 2.92, 1.75, 3.5, 2.33, 2.91, 2.5, 3.25, 2.64, 3.25, 2.83, 2.08,
> 2.25, 2.17, 2.42, 2.58, 2.42, 2.58, 2.42, 3, 3, 2.83, 2.67, 3.58,
> 1.58, 2.83, 2.83, 2.67, 3.17, 2.42, 1.92, 2.92, 2.5, 2.42, 2.42,
> 2.58, 2.42, 3.33, 3, 3.17, 2.17, 2.58, 2.67, 2.58, 3.75, 2.5,
> 2.08, 2.25, 3.25, 3.17, 2.91, 2.08, 2.25, 3.08, 2.91, 3.08, 2.92,
> 1.83, 2.5, 2.5, 2.83, 2.67, 3.33, 2.83, 3.33, 2.92, 3), Levels_Depression =
> c(1.3,
> 1.71, 3.08, 0.48, 0.51, 0.71, 1.37, 0.2, 1.21, 1.07, 2.8, 1.24,
> 0.46, 0.97, 0.81, 1.13, 1.58, 3.12, 1.8, 1.54, 1.02, 0.32, 2.63,
> 1.39, 1.34, 2.37, 2.6, 1.11, 1.59, 2.17, 1.99, 0.59, 0.76, 0.23,
> 2.22, 1.98, 0.41, 0.32, 0.37, 1.11, 2.29, 0.97, 1.61, 1.27, 1.22,
> 2.38, 1.28, 1.21, 0.93, 2.3, 0.8, 2.1, 2.86, 2.47, 2.34, 2.67,
> 0.31, 0.88, 1.84, 0.23, 2.41, 0.56, 2.03, 1.11, 0.12, 2.39, 0.34,
> 2.08, 1.01, 1.51), Age = c(66, 59, 49, 70, 42, 55, 28, 41, 69,
> 65, 40, 21, 18, 77, 28, 40, 47, 37, 47, 39, 32, 33, 42, 28, 59,
> 49, 29, 41, 22, 29, 53, 39, 55, 61, 30, 49, 43, 46, 18, 36, 34,
> 17, 42, 37, 37, 54, 48, 23, 71, 42, 52, 83, 19, 47, 23, 80, 43,
> 38, 47, 80, 36, 73, 74, 51, 76, 14, 65, 39, 17, 73), Past_Admissions = c(1,
> 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1,
> 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
> 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
> 1, 0, 1, 0, 0, 1)), .Names = c("Social_impairment", "Levels_Depression",
> "Age", "Past_Admissions"), row.names = c(NA, 70L), class = "data.frame")
>
> dim(df)
> head(df)
> str(df)
> summary(df)
>
>
> ### call the library
>
> library(ggplot2)
>
>
> #### the plot
>
>
> #### Levels_Depression on Social_impairment by Past_Admissions (yes/no)
> #### linear model
> #### radius of the bubbles proportional to age
>
>
> #### background elimination
>
> p1 <- ggplot(data = df, aes(x =Levels_Depression, y = Social_impairment,
> group = as.factor(Past_Admissions), col = as.factor(Past_Admissions))) +
>   geom_point(aes(size = Age)) + geom_smooth(method = "lm") + xlab("Levels
> of depression") + ylab("Social impairment") +
>   scale_colour_discrete("History of \npast admissions\nto a psychiatric
> service", labels = c("No", "Yes"))
>
> p1 + theme(panel.grid.major = element_blank(), panel.grid.minor =
> element_blank(),
>     panel.background = element_blank(), axis.line = element_line(colour =
> "black"))
>
>
> ### change of then axes' ticks
>
> p1 + theme(panel.grid.major = element_blank(), panel.grid.minor =
> element_blank(),
>     panel.background = element_blank(), axis.line = element_line(colour =
> "black"),
>     axis.text = element_text(color = "black", size = 12, face = "italic"))
>
>
> ### after saving, dev.off()
> ###
>
>
> #### Age on Social_impairment by Past_Admissions (yes/no)
> #### linear model
> #### radius of the bubbles proportional to Levels_Depression
>
>
>
> #### background elimination
>
> p2 <- ggplot(data = df, aes(x =Age , y = Social_impairment, group =
> as.factor(Past_Admissions), col = as.factor(Past_Admissions))) +
>   geom_point(aes(size = Levels_Depression)) + geom_smooth(method = "lm")
> +xlab("Age of participants") + ylab("Social impairment") +
>   scale_colour_discrete("History of \npast admissions\nto a psychiatric
> service", labels = c("No", "Yes"))
>
> p2 + theme(panel.grid.major = element_blank(), panel.grid.minor =
> element_blank(),
>     panel.background = element_blank(), axis.line = element_line(colour =
> "black"))
>
>
> ### change of then axes' ticks
>
> p2 + theme(panel.grid.major = element_blank(), panel.grid.minor =
> element_blank(),
>     panel.background = element_blank(), axis.line = element_line(colour =
> "black"),
>     axis.text = element_text(color = "black", size = 12, face = "italic"))
>
>
> ### after saving, dev.off()
> ###
>
>
> ########################
> #### paired plots
> ########################
>
> library(gridExtra)
>
> grid.arrange(p1, p2, ncol = 2)
>
>
>
> ########################
> ### sessionInfo()
> ########################
>
> R version 3.0.2 (2013-09-25)
> Platform: x86_64-w64-mingw32/x64 (64-bit)
>
> locale:
> [1] LC_COLLATE=Italian_Italy.1252  LC_CTYPE=Italian_Italy.1252
> [3] LC_MONETARY=Italian_Italy.1252 LC_NUMERIC=C
> [5] LC_TIME=Italian_Italy.1252
>
> attached base packages:
> [1] grid      stats     graphics  grDevices utils     datasets  methods
> [8] base
>
> other attached packages:
> [1] gridExtra_0.9.1 ggplot2_0.9.3.1
>
> loaded via a namespace (and not attached):
>  [1] colorspace_1.2-4   dichromat_2.0-0    digest_0.6.4
> gtable_0.1.2
>  [5] labeling_0.2       MASS_7.3-33        munsell_0.4.2
> plyr_1.8
>  [9] proto_0.3-10       RColorBrewer_1.0-5 reshape2_1.2.2
> scales_0.2.3
> [13] stringr_0.6.2
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help op r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list