我正在尝试将我的绘图的y轴缩放为看起来像正态分布。例如,像plot一样。
我的代码输出给了我plot。
不存在等效的“scale_y_normal”,所以我想知道是否有人有想法这样做?我提出了完整性的代码,但这纯粹是一个ggplot2问题,所以请只关注我代码的最后几行。谢谢!
library(foreign) # load foreign package (converts data files into R)
library(survey) # load survey package (analyzes complex design surveys)
library(ggplot2)
#-----------------------------------Download and importation function
download.nhanes.file <- function( ftp.filepath ){
# create a temporary file for downloading file to the local drive
tf <- tempfile()
# download the file using the ftp.filepath specified
download.file(
# download the file stored in the location designated above
ftp.filepath ,
# save the file as the temporary file assigned above
tf ,
# download this as a binary file type
mode = "wb"
)
# the variable 'tf' now contains the full file path
# on the local computer to the specified file
read.xport( tf ) # the last line of a function contains what it *returns*
# so by putting read.xport as the final line,
# this function will return an r data frame
}
#-----------------------------------Download files and creata data.frame
x1112 <-
download.nhanes.file("ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/2011-2012/GLU_G.xpt")
x0910 <-
download.nhanes.file("ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/2009-2010/GLU_F.xpt")
x0708 <-
download.nhanes.file("ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/2007-2008/GLU_E.xpt")
x0506 <-
download.nhanes.file("ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/2005-2006/GLU_D.xpt")
x0304 <-
download.nhanes.file("ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/2003-2004/L10AM_C.xpt")
x0102 <-
download.nhanes.file("ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/2001-2002/L10AM_B.xpt")
x9900 <-
download.nhanes.file("ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/1999-2000/LAB10AM.xpt")
#Rename to 'LBDGLUSI' (only 99-02)
colnames(x0102)[5] <- 'LBDGLUSI'
colnames(x9900)[5] <- 'LBDGLUSI'
#Add extra column for year
x1112[,3] <- '11-12'
x0910[,3] <- '09-10'
x0708[,3] <- '07-08'
x0506[,3] <- '05-06'
x0304[,3] <- '03-04'
x0102[,3] <- '01-02'
x9900[,3] <- '99-00'
#Name the varaible YEAR
colnames(x1112)[3] <- 'YEAR'
colnames(x0910)[3] <- 'YEAR'
colnames(x0708)[3] <- 'YEAR'
colnames(x0506)[3] <- 'YEAR'
colnames(x0304)[3] <- 'YEAR'
colnames(x0102)[3] <- 'YEAR'
colnames(x9900)[3] <- 'YEAR'
#Stack all data.frames on top of each other
final <- rbind(x1112[,c("LBDGLUSI", "YEAR")], x0910[,c("LBDGLUSI", "YEAR")], x0708[,c("LBDGLUSI", "YEAR")],
x0506[,c("LBDGLUSI", "YEAR")], x0304[,c("LBDGLUSI", "YEAR")], x0102[,c("LBDGLUSI", "YEAR")], x9900[,c("LBDGLUSI", "YEAR")])
#-----------------------------------Plot cumulative fasting blood glucose by YEAR
ggplot(final, aes(x=final$LBDGLUSI, color=YEAR)) +
geom_point(aes(y=..y..),stat="ecdf") +
xlab(expression(paste('Serum Glucose mM'))) +
ylab('Fraction of the Population') +
ggtitle('NHANES Fasting Blood Glucose') +
coord_trans(x = 'log10')