Upananda Pani
2022-Dec-26 14:06 UTC
[R] Reg: Help in assigning colors to factor variable in ggplot2
Dear All, I am trying to plot a scatter plot between temperature and heart rate and additionally marking the outcome of the patients by colors. I am using the standard package Use the standard function plot as well as the functions of package "ggplot2" (Wickham (2009)). Save the plots in pdf files. I am geeting an error to plot when assigning colsOutcome to the scatterplot. I am doing it wrongly. Please advise me. ```{r} library(ggplot2) library(RColorBrewer) library(ggsci) ICUData <- read.csv(file = "ICUData.csv") ``` ```{r} ## Generate empty vector colsOutcome <- character(nrow(ICUData)) ## Fill with colors colsOutcome[ICUData$outcome == "died"] <- "#E41A1C" colsOutcome[ICUData$outcome == "home"] <- "#377EB8" colsOutcome[ICUData$outcome == "other hospital"] <- "#4DAF4A8" colsOutcome[ICUData$outcome == "secondary care/rehab"] <- "#984EA3" ``` ```{r} plot(x = ICUData$temperature, y = ICUData$heart.rate, pch = 19, xlab = "Maximum body temperature", ylab = "Maximum heart rate", main = "500 ICU patients", col = colsOutcome, xlim = c(33,43)) legend(x = "topleft", legend = c("died", "home", "other hospital", "secondary care/rehab"), pch = 19, col = c("#E41A1C", "#377EB8", "#4DAF4A8", "#984EA3")) ```
Eric Berger
2022-Dec-26 14:28 UTC
[R] Reg: Help in assigning colors to factor variable in ggplot2
Your problem is that the color "#4DAF4A8" is not a valid color. It should have only 6 hex digits. e.g. "#4DAF4A" will work. Also you seem to be confused about ggplot. You are not using ggplot in your code at all. You are using base graphics. You can remove the 3 library calls. HTH, Eric On Mon, Dec 26, 2022 at 4:06 PM Upananda Pani <upananda.pani at gmail.com> wrote:> Dear All, > > I am trying to plot a scatter plot between temperature and heart rate and > additionally marking the outcome of the patients by colors. I am using the > standard package Use the standard function plot as well as the functions of > package "ggplot2" (Wickham (2009)). Save the plots in pdf files. > > I am geeting an error to plot when assigning colsOutcome to the > scatterplot. I am doing it wrongly. Please advise me. > ```{r} > library(ggplot2) > library(RColorBrewer) > library(ggsci) > ICUData <- read.csv(file = "ICUData.csv") > ``` > ```{r} > ## Generate empty vector > colsOutcome <- character(nrow(ICUData)) > ## Fill with colors > colsOutcome[ICUData$outcome == "died"] <- "#E41A1C" > colsOutcome[ICUData$outcome == "home"] <- "#377EB8" > colsOutcome[ICUData$outcome == "other hospital"] <- "#4DAF4A8" > colsOutcome[ICUData$outcome == "secondary care/rehab"] <- "#984EA3" > ``` > > ```{r} > plot(x = ICUData$temperature, y = ICUData$heart.rate, pch = 19, > xlab = "Maximum body temperature", ylab = "Maximum heart rate", > main = "500 ICU patients", col = colsOutcome, xlim = c(33,43)) > legend(x = "topleft", legend = c("died", "home", "other hospital", > "secondary care/rehab"), pch = 19, > col = c("#E41A1C", "#377EB8", "#4DAF4A8", "#984EA3")) > ``` > ______________________________________________ > 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. >[[alternative HTML version deleted]]
John Kane
2022-Dec-26 14:46 UTC
[R] Reg: Help in assigning colors to factor variable in ggplot2
I suspect you may be mixing *plot()* commands with *ggplot()* commands and they are likely incompatible. Could you supply some sample data and any error messages that you are getting? A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. On Mon, 26 Dec 2022 at 09:06, Upananda Pani <upananda.pani at gmail.com> wrote:> Dear All, > > I am trying to plot a scatter plot between temperature and heart rate and > additionally marking the outcome of the patients by colors. I am using the > standard package Use the standard function plot as well as the functions of > package "ggplot2" (Wickham (2009)). Save the plots in pdf files. > > I am geeting an error to plot when assigning colsOutcome to the > scatterplot. I am doing it wrongly. Please advise me. > ```{r} > library(ggplot2) > library(RColorBrewer) > library(ggsci) > ICUData <- read.csv(file = "ICUData.csv") > ``` > ```{r} > ## Generate empty vector > colsOutcome <- character(nrow(ICUData)) > ## Fill with colors > colsOutcome[ICUData$outcome == "died"] <- "#E41A1C" > colsOutcome[ICUData$outcome == "home"] <- "#377EB8" > colsOutcome[ICUData$outcome == "other hospital"] <- "#4DAF4A8" > colsOutcome[ICUData$outcome == "secondary care/rehab"] <- "#984EA3" > ``` > > ```{r} > plot(x = ICUData$temperature, y = ICUData$heart.rate, pch = 19, > xlab = "Maximum body temperature", ylab = "Maximum heart rate", > main = "500 ICU patients", col = colsOutcome, xlim = c(33,43)) > legend(x = "topleft", legend = c("died", "home", "other hospital", > "secondary care/rehab"), pch = 19, > col = c("#E41A1C", "#377EB8", "#4DAF4A8", "#984EA3")) > ``` > ______________________________________________ > 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. >-- John Kane Kingston ON Canada [[alternative HTML version deleted]]