adding x=y line to hexplot in ggplot2 - r

Hi I am trying to look at different ways to visualize a dataset by using hexplots in ggplot.
I essentially want a hexplot with
1. loess line
2. regression line
3.x=y line --> equivalent of abline(0,1)
So far I have come up with this kind of code:
c <- ggplot(mtcars, aes(qsec, wt))
c+stat_binhex()+stat_smooth(method="loess", colour="red")+stat_smooth(method='lm', se=FALSE, colour="orange")+ geom_abline(intercept=0, slope=1)
This gives the picture below, but I still do not see the x=y reference line. Please help. I'm not sure why it is not working. Thanks

The y=x reference line is not inside the coordinate ranges you plot. If you change to
geom_abline(slope=1, intercept=-15)
you will see your line on the plot.

Related

How to avoid broken line in ggplot2

I try to plot a graph in R using the ggplot2 package. Let's assume I have the following data:
df1<-data.frame(xpos=c(1,2,3,4),ypos=c(0.222,0.222,0.303,0.285))
Now, I want to plot a simple line graph:
ggplot(data=df1, aes(x=xpos, y=ypos)) +
geom_line()+
geom_point()
Now, when I adjust the y-axis:
+scale_y_continuous("Y rates upd", breaks=seq(0,1,0.2),limits=c(0,1))
The lines get "broken"
That's dependent upon your graphics device that plots are sent to from R. See the same plot below when I instead export it to PDF.

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()

Suppress the legend in a ggplot2/qplot density plot

I've looked through the description and the book on ggplot2 and cannot find a simple way of eliminating the legend in a simple density plot with a filled color.
Here is what I've tried with a simple sequence of 1000 numbers (plotseries) that had about 200
NA in the first 200 spots.
qplot(plotseries,geom="density",fill="red",na.rm=TRUE,show_guide=FALSE)
qplot(plotseries,geom="density",fill="red",na.rm=TRUE,legend.position="none")
I looked at the online ggplot2 doc and could not find anything there either....
If you just use the normal qplot command and then add + theme(legend.position = "none") to your code, it will remove the legend. So, your code will look as follows:
qplot(plotseries,geom="density",fill="red",na.rm=TRUE) + theme(legend.position="none")
Usually such things work just as they would work in the ggplot2 command.

How to use ggplot in R to create a single graphic comprised of a bar plot and a line plot?

I am new to using qplot and ggplot, and basically want to make a figure that is just the combination of a bar plot and a line plot. I can do one or the other, but don't know how to do both at once!
Here is my data:
bulk = data.frame(x_pos=c(1,2,3,4,5,6,7,8),
y_line=c(3,7,6,8,14,16,18,12),
y_bar=c(0,0,10,0,0,0,10,0))
For a line graph, I just do qplot(x_pos, y_line, data=bulk, geom="line")
For a bar plot, I just do qplot(x_pos, y_bar, data=bulk)
But! How can I combine these at once into a single figure?? My real intention is to use several (maybe 6-10) different graphics techniques like this to generate complex figures, but it all starts with knowing how to do two at once. Thanks for any help!
Don't use qplot for this.
library(ggplot2)
ggplot(bulk, aes(x=x_pos)) +
geom_bar(aes(y=y_bar), stat="identity") +
geom_line(aes(y=y_line), color="red", size=2)

How to plot a graph of Probability density function using ggplot

Here is my data set
link to dataset
I want to plot a graph showing the Probability density function for the variable quality of the division on the type of wine.
I try this:
library(ggplot2)
db <- dbeta(wines$quality, 1, 1)
qplot(wines$quality, db, geom="line")
but it plot flat line.
ok, i think my code don't have any sens. I want to do somethink lie that:
Example
x-quality of wines(dry,semi-dry....)
What can I do?
Is this what you want?
ggplot(wines) + geom_density(aes(quality))
EDIT:
I see your point, but probably you just need to rescale the y values (am I correct?)
so is not this what you're after? changed the image
ggplot(wines[-4381,]) + geom_density(aes(x=quality)) +
facet_wrap(~sweetnes)
or all in one with different fill
ggplot(wines) + geom_density(aes(x=quality, fill=sweetnes))

Resources