ggplot2 error in plotting a scatter plot in R - r

I expect this kind of scatter plot.
However, whenever I tried to apply on my data, I get this.
I just used this code, and this is my data.
And I also confirmed they are numeric class.
ggplot(selected.df, aes(x, y))
making a right plot.

Those variables were not numeric.

Related

Plotting princomp loadings with ggplot

I'm trying to carry out a PCA Analysis on my dataset, and I can plot the loadings out on the basic plot function. But I want to do them on ggplot, but I keep getting this error, "ggplot2 doesn't know how to deal with data of class loadings".
I'm using the princomp function, and I'm plotting my first component against my second component. And I need to colour it on the basis of an external factor which has the same rownames as the dataframe I'm carrying out the pca on. I've tried to do the same thing as this Tutorial with prcomp, but with pca$loadings, but it didnt work. I need to plot my first PC against my second PC in ggplot.
Blockquote
xy <- princomp(iris[,-5])
plot(xy$loadings[,"Comp.1"], xy$loadings[,"Comp.2"], col=iris$Species)
ggplot(xy, aes(x=xy$loadings[,1], y=xy$loadings[,2]))+geom_point()
ggplot(as.data.frame(xy$loadings[,1:2]), aes(x=Comp.1, y=Comp.2))+geom_point()

Trouble producing a polygon on top of a scatterplot using ggplot

Currently, I am trying to transition my graphical knowledge from the plot function in R, to the ggplot function. I have began constructing scatterplots and corresponding legends for a given data set, however I want to incorporate the function geom_polygon onto my plots using ggplot.
Specifically, I want to capture a triangular region from the origin of a scatterplot. For reproducibility, say I have the following data set:
rawdata<-data.frame(matrix(c(1,1,1,
2,1,-1,
3,-1,-1,
4,-1,1,
4,-2,2),5,3,byrow=TRUE))
names(rawdata)<-c("Town","x.coordinate","y.coordinate")
rawdata[,1]<-as.factor(rawdata[,1])
To construct a scatterplot along with a legend, I have been told to do the following:
p1<-ggplot(data=rawdata,aes(x=x.coordinate,y=y.coordinate,colour=Town,shape=Town))
+ theme_bw() + geom_point()
The result is the following:
Click here.
What I want to do now is produce a polygon. To do so, I have construct the following dataframe to use in the geom_polygon function:
geom_polygon(data=polygondata,aes(x = xa, y = ya),colour="darkslategray2",
fill = "darkslategray2",alpha=0.25)
However, when I combine this with p1, I get the following error:
Error in eval(expr, envir, enclos) : object 'Town' not found
From some messing around, I have noticed that when I omit the shape argument from the ggplot function, I can easily produce the desired output which is shown here. However, I wish to keep the shape for aesthetics.
I also get a similar problem when I try to produce arrows which connect points on the scatterplot using ggplot. However, I will address this problem after, as the root problem may be here.
Add the following to polygondata:
polygondata$Town = NA
Even though you're not using that variable in geom_polygon, ggplot expects it to be there if that column is used for an aesthetic in the main call to ggplot.
Alternatively, I think you could avoid the error if you move the aesthetic mapping in the initial plot to geom_point rather than the main ggplot call, like this:
p1 <- ggplot(data=rawdata) +
theme_bw() +
geom_point(aes(x=x.coordinate, y=y.coordinate, colour=Town, shape=Town))
In that case, you wouldn't need to add a Town column to polygondata.

Trouble producing discrete legend using ggplot for a scatterplot

I am fairly new to the ggplot function in R. Currently, I am struggling to produce a legend for a given data set that I have constructed by hand. For simplicity, suppose this was my data set:
rawdata<-data.frame(matrix(c(1,1,1,
2,1,-1,
3,-1,-1,
4,-1,1
4,-2,2),5,3,byrow=TRUE))
names(rawdata)<-c("Town","x-coordinate","y-coordinate")
rawdata[,1]<-as.factor(rawdata[,1])
Now, using ggplot, I am trying to figure out how to produce a legend on a scatterplot. So far I have done the following:
p1<-ggplot(data=rawdata,aes(x=x.coordinate,y=y.coordinate,fill=rawdata[,1]))
+geom_point(data=rawdata,aes(x=x.coordinate,y=y.coordinate))
I produce the following using the above code,
As you can see, the coordinates have been plotted and the legend has been constructed, but they are only colored black.
I learned that to color coordinates, I would have needed to use the argument colour=rawdata[,1] in the geom_point function to color in points. However, when I try this, I get the following error code:
Error: Aesthetics must be either length 1 or the same as the data (4): colour
I understand that this has something to do with the length of the vector, but as of right now, I have absolutely no idea how to tackle this small problem.
geom_point() takes a colour, not a fill. And, having passed the data into ggplot(data = ..), there's no need to then pass it into the geom_point() again.
I've also fixed an error in the creation of your df in your example.
rawdata<-data.frame(matrix(c(1,1,1,2,1,-1,3,-1,-1,4,-1,1,4,-2,2),5,3,byrow=TRUE))
names(rawdata)<-c("Town","x.coordinate","y.coordinate")
rawdata[,1]<-as.factor(rawdata[,1])
library(ggplot2)
ggplot(data=rawdata,aes(x=x.coordinate,y=y.coordinate,colour=Town)) +
geom_point()

aesthetic mapping in ggplot inside loop

I was trying to plot multiple time-series continuous variables in a single plot using ggplot2. As there were a lot of variable and I tried to use normal aesthetic mapping inside for-loop as,
p1<-ggplot(df, aes(x=timVar))
ind<-c(2,4,5,6,8,9,10,12,13,15,17) # Index of the series that I wanted to plot
for(i in ind){
p1<-p1+geom_line(aes(df[,i]))
}
print(p1)
Since this gave me only the plot of last series and I googled for some solution and finally found one which had suggested me to use aes_string() function. I rebuild the code as,
p1<-ggplot(df, aes(x=timVar))
ind<-c(2,4,5,6,8,9,10,12,13,15,17) # Index of the series that I wanted to plot
for(i in ind){
p1<-p1+geom_line(aes_string(names(df)[i]))
}
print(p1)
This gave me all the lines I needed. However, when I tried to get separate color for each variables, I could not get the discrete color. I used the following code,
p1<-p1+geom_line(aes_string(names(df)[i], col=names(df)[i]))
Is there any way to use aes_string and aes together inside the loop or is there any way to generate discrete color values with label to be the variable names.
I am using the melt function of dplyr package. It solved all my problem above. Thanks for all your comments.

Why I get error, when I'm trying to have overlapped density plot using ggplot2?

I want to create overlaped density plot. I decided to use ggplot2.
My data are in data frame formate.
Here How they are look:
Ge<-data.frame(Ge)
dim(Ge)
#[1] 100 1
Ge[1:4,]
#[1] 6.005409 38.681342 102.079283 185.672611
dim(Tr)
#[1] 100 1
Tr[1:4,]
#[1] 12.8678547 1.3034715 1.1372413 0.7973491
Here is my code to create plot:
library(ggplot2)
ggplot() + geom_density(aes(x=x), colour="red", data=Tr) +
geom_density(aes(x=x), colour="blue", data=Ge)
But this is the error I get it:
Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous
Error: stat_density requires the following missing aesthetics: x
Would someone help me to solve this ?
You should be using a single data frame where ever possible with ggplot. That is the logic behind the syntax, but is unintuitive at first. Considering your sample code, Tr and Ge are factors and there is one set of values which you're representing on a common x-axis.
The reshape2 package has a handy tool for combining separate data into a format suitable for ggplot plotting, melt. Check out the package documentation, but see below for working code and a sample output.
require(ggplot2)
require(reshape2)
Ge=runif(n=100)
Tr=runif(n=100)
data=data.frame(Ge,Tr)
names(data)=c('Ge','Tr')
data=melt(data,id.vars=NULL)
ggplot(data,aes(x=value,fill=variable))+geom_density(alpha=.4)
There is a book by Hadley Wickham which covers all of this information in excellent detail. Amazon link
Update
I have more closely replicated the OP's code (straying away from best practices) and still get a functional plot, though with a warning.
Ge=data.frame(runif(n=100))
Tr=data.frame(runif(n=120))
ggplot()+geom_density(aes(data=Ge,x=Ge[,1]),color='red')+
geom_density(aes(data=Tr,x=Tr[,1]),color='blue')
Don't know how to automatically pick scale for object of type
data.frame. Defaulting to continuous Don't know how to automatically
pick scale for object of type data.frame. Defaulting to continuous

Resources