服务热线
178 0020 3020
R2-03作业之ggplot2画直方图、密度图
任务1:
library(ggplot2)
data=read.csv(file.choose())
data
data$smoke=factor(data$smoke)
p<-ggplot(data,aes(x=bwt,fill=smoke))+
geom_histogram(binwidth=400,position="dodge",color="black")+
xlab("birth weight")+
labs(title="R2-03-2")+
scale_fill_discrete(labels=c("No","Yes"))
ggsave("C:/Users/hw/Downloads/R2-第2期任务/R2-03-2.png")
library(ggplot2)
mydata<-read.csv("C:/Users/hw/Downloads/R2-第2期任务/R2-2.1.csv",header=T)
mydata$smoke=factor(mydata$smoke)
a<-ggplot(mydata,aes(x=bwt,color=smoke))+
geom_density()+
xlab("birth weight")+
labs(title="R2-03-3")
ggsave("C:/Users/hw/Downloads/R2-第2期任务/R2-03-3.png",width=6,height=4)
任务2:
library(ggplot2)
ggplot(diamonds, aes(x=price, color= color))+
geom_line(stat = "density",size=1) +
xlab("price")+
ggtitle("R2-03-4")+
theme_bw()
ggsave("C:/Users/hw/Downloads/R2-第2期任务/R2-03-4.png",width=6,height=4)
附件