[R] colours in ggplot2

Jose Iparraguirre Jose.Iparraguirre at ageuk.org.uk
Mon Feb 23 12:44:01 CET 2015


Dear Antonello,

How about this? Obviously, you can change the background colours for the confidence levels (deprespal) and for the regression lines, etc.
Surely, many in the list can improve on my rough and ready solution.

deprespal=c("#CCCCCC","#999999")
p2 <- ggplot(data = df, aes(x =Levels_Depression, y = Social_impairment, group = as.factor(Past_Admissions),fill = as.factor(Past_Admissions) )) +
    geom_point(aes(size = Age)) + geom_smooth(method = "lm", col="black") + xlab("Levels of depression") + ylab("Social impairment") +
    scale_fill_manual(values=deprespal) +
    scale_colour_discrete("History of \npast admissions\nto a psychiatric service", labels = c("No", "Yes"))
p2

Regards,

José

Prof. José Iparraguirre
Chief Economist
Age UK

Age UK
Tavis House, 1- 6 Tavistock Square
London, WC1H 9NB


-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Antonello Preti
Sent: 20 February 2015 12:54
To: r-help at r-project.org
Subject: [R] colours in ggplot2

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 at 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.

Age UK Group

Age UK is a registered charity and company limited by guarantee, (registered charity number 1128267, registered company number 6825798) Registered office: Tavis House, 1-6 Tavistock Square, London WC1H 9NA. 

For the purposes of promoting Age UK Insurance, Age UK is an Appointed Representative of Age UK Enterprises Limited. Age UK Enterprises Limited is authorised and regulated by the Financial Conduct Authority.

Charitable Services are offered through Age UK (the Charity) and commercial products and services are offered by the Charity’s subsidiary companies. The Age UK Group comprises of Age UK, and its subsidiary companies and charities, dedicated to improving the lives of people in later life. Our network includes the three national charities Age Cymru, Age NI and Age Scotland and more than 160 local Age UK charities.

This email and any files transmitted with it are confide...{{dropped:11}}



More information about the R-help mailing list