调整绘图代码以制作ggplot

时间:2018-05-23 18:56:03

标签: r ggplot2

我有以下数据

[1] 0.09733344 0.17540020 0.14168188 0.54093074 0.78151039 0.28068527 [7] 1.96164429 0.33743328 0.05200734 0.09103039 0.28842044 0.09240131 [13] 0.09143535 0.38142022 0.11700952

我从中做了贝叶斯推理并用以下代码制作了一个情节

f_theta<-function(theta,Data){
(theta^length(Data) )*exp(-theta*sum(Data))}
theta<-seq(1,20,length=100)
a=b=0.001
plot(theta,dgamma(theta,a,b),type="l",col="red",
ylim=c(0,2),tck=-0.01,cex.lab=0.8,cex.axis=0.8)
lines(theta,dgamma(theta,length(Data)+a,sum(Data)+b),col="green",lty=1)
lines(theta,f_theta(theta,Data=Data),lty=1,col="blue")
legend('topright',legend=c("Prior","Post","Likelihood")
 ,col=c("red","green","blue","purple"),lty=1,bty="n",cex=0.8)

但我看过以下图表

enter image description here

有代码

# ggplot2 examples
library(ggplot2) 

# create factors with value labels 
mtcars$gear <- factor(mtcars$gear,levels=c(3,4,5),
labels=c("3gears","4gears","5gears")) 
mtcars$am <- factor(mtcars$am,levels=c(0,1),
labels=c("Automatic","Manual")) 
mtcars$cyl <- factor(mtcars$cyl,levels=c(4,6,8),
labels=c("4cyl","6cyl","8cyl")) 

# Kernel density plots for mpg
# grouped by number of gears (indicated by color)
qplot(mpg, data=mtcars, geom="density", fill=gear, alpha=I(.5), 
main="Distribution of Gas Milage", xlab="Miles Per Gallon", 
ylab="Density")

但是我不太熟悉ggplot库和图表,我想要一些帮助以便调整我的代码并制作类似于最后一个的图形。

0 个答案:

没有答案