colouring plot by factor using custom colours in r [duplicate] - r

This question already has answers here:
Colouring plot by factor in R
(6 answers)
Closed 9 years ago.
I am trying to make a scatter plot coloured by factor. I am using the following code:
data<-iris
plot(data$Sepal.Length, data$Sepal.Width, col=data$Species)
Is there anyway I can colour by the species factor but specify my own custom colours? Having a look around on Google it seems it is possible to do using ggplot2 but I have never used it and was hoping I could do this using the basic R functions.
Any help would be greatly appreciated!

You can manually set the R palette used by your plot call like so:
palette(c("blue","pink","green"))
Which you can reset like so:
palette("default")
Try it out, creating two plots, one with default colours, one with the new colours specified:
# default plotting
palette("default")
plot(iris$Sepal.Length, iris$Sepal.Width, col=iris$Species, pch=19)
# after specifying custom palette
palette(c("blue","pink","green"))
plot(iris$Sepal.Length, iris$Sepal.Width, col=iris$Species, pch=19)

Related

How to address overplotting in GGally::ggpairs()? [duplicate]

This question already has answers here:
Change colors in ggpairs now that params is deprecated
(2 answers)
Closed 1 year ago.
I'm using the ggpairs() function from the GGally package, but I'm having an issue dealing with overplotting.
Is there a good way to address the overplotting? I've tried setting the alpha value but I couldn't find out how to pass it to ggpairs(). I also looked into using geom_hex but again, I couldn't see how to use it with ggpairs().
Here's a simple example:
# Create example data
df <- data.frame(x1=rnorm(1e4),
x2=rnorm(1e4),
x3=runif(1e4))
# Pairs plot
GGally::ggpairs(df)
To reduce overplotting of the points you may modify the size aesthetic in point based layers displayed in the lower triangular of the plot matrix:
GGally::ggpairs(df, lower=list(continuous=GGally::wrap("points", size = .01)))

ggplot2 - making a stick plot (plotting a mass spectrum) [duplicate]

This question already has an answer here:
Needle plot in ggplot2 [duplicate]
(1 answer)
Closed 8 years ago.
I am trying to make a plot x,y stick plot (like the link in the bottom of the post). I am attempting to make a Mass spectrum plot (basically a stick plot), is there an easy way of doing this with ggplot2? I know how to make the plot with the plot() function in R, however, I would like to be able to make it with ggplot 2, as I need to do some additional modifications afterwards for which I require ggplot2.
So essentially I want to make a x,y stick plot. I am new to R, so bear with me.
Example of what I want
Try to upload some code and let us know where you are getting stuck. Also, please refer to the documentation or these examples.
That having been said, I believe this is close to what you want:
library(ggplot2)
ggplot(data = mtcars, aes(x = mpg)) +
geom_bar(binwidth = 1, color = 'white')

ordering bars in geom_bar position=fill [duplicate]

This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Closed 8 years ago.
I tried to finde a solution for my problem but I could not. Probably, my problem is easy for some of you. However, I need support. I would be greatful for any help.
I have made a ggplot for two factors: HGU_type and cycle_complexity to show their proportions:
I used:
g2<-ggplot(t,aes(x=HGU_type,fill = cycle_complexity))+ geom_bar(position="fill")
g2
The graph, I get looks as follow:
I want to have increasing order of the bars on the x-axis...first bar with "nod", second with "shake", third with "retr"...
I tried everything and cannot find the solution.
I would be grateful for a hint
As #Japp pointed out, it's always best to include a minimal reproducible example with your question. I created this data set
#sample data
set.seed(18)
t<-data.frame(
HGU_type=sample(c("jerk","nod","pro","retr","shake","tilt","turn"), 50, replace=T, prob=sample(7)),
cycle_complexity=sample(c("multiple", "single"), 50, replace=T)
)
And a plot like your original one is created by
ggplot(t,aes(x=HGU_type,fill = cycle_complexity))+ geom_bar(position="fill")
In order to change the order in which the bars are drawn, you need to change the levels of the factor used for the x-axis. The reorder() function makes it easy to reorder factors based on different properties. Here we will re-order based on the proportion of "multiple" in each group
t$HGU2<-reorder(t$HGU_type, t$cycle_complexity,FUN=function(x) mean(as.numeric(x)))
Then we can plot with
ggplot(t,aes(x=HGU2,fill = cycle_complexity))+ geom_bar(position="fill")
to get

Embedding output from ggplot into another plot [duplicate]

This question already has answers here:
Combined plot of ggplot2 (Not in a single Plot), using par() or layout() function? [duplicate]
(3 answers)
R: How should I create Grid-graphics?
(2 answers)
Closed 8 years ago.
I am trying to plot a scatterplot and box plot of two continuous varaibles but am getting an error that says,
Warning message:
In par(fig = c(0, 0.8, 0.55, 1), new = TRUE) :
calling par(new=TRUE) with no plot
The code worked when I simply replaced lines 4-6 of my code with:
plot(mydata$gre, mydata$gpa, xlab="GRE",ylab="GPA")
Here's my code:
mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
par(fig=c(0,0.8,0,0.8), new=TRUE)
#plot(mydata$gre, mydata$gpa, xlab="GRE",ylab="GPA")
d<-ggplot(mydata,aes(x=mydata$gre, y=mydata$gpa))
d<-d+geom_line()
d
par(fig=c(0,0.8,0.55,1), new=TRUE)
boxplot(mydata$gre, horizontal=TRUE, axes=FALSE)
par(fig=c(0.65,1,0,0.8),new=TRUE)
boxplot(mydata$gpa, axes=FALSE)
mtext("Enhanced Scatterplot", side=3, outer=TRUE, line=-3)
Could you please shed some light on what I am doing wrong with regards to ggplot since R isn't recognizing it? What's really weird is that when I type d, the name of my ggplot, I get a plot...
You are attempting to combine grid plots (ggplot2) and base plots (boxplot), but those 2 types of plotting do not play nicely together (hence the cryptic warning).
The simplest solution is to only use one of grid or base plots by replacing the call to ggplot with a call to plot or other functions (base only option) or using grid based functions to do the boxplots (lattice package also uses grid) then use functions from the grid package to arrange the multiple plots.
If you really want to combine grid and base plots then you can use the gridBase package, but this is going to require understanding both types of graphics quite well.

How to make a scatter-plot if I have more than one treatment? [duplicate]

This question already has answers here:
How to create a line plot with groups in Base R without loops?
(3 answers)
Closed 9 years ago.
here is my question.
I would like to make a scatterplot which (x,y) pairs are connected by line.
I have three treatments A,B,and C
A<-c(103.4,102.5,101.4,101.0,98.8)
B<-c(102.9,101.6,101.4,100.3,99.6)
C<-c(103.9,103.1,102.3,100.4,97.6)
These 3 variables are going to be plotted on the y axis (Temperature)
And I also have a variable for the x axis (Minutes)
M<-c(15,30,45,60,75)
I just would like to know how to make a plot using different lines and symbols for the 3 different treatments.
Thanks so much!
png() # default file name is "Rplot001.png" in your working directory
matplot(M, cbind(A,B,C), type="b",
xlab= "Minutes", ylab=expression(Temperature~degree*F),
xlim=range(M)+c(-5,5), ylim=range(c(A,B,C)) +c(-1,1) )
dev.off()
# See ?legend and ?title for further annotation

Resources