2015-10-26 55 views
0

我试图绘制的包络线:如何获得正常尺度下的包络曲线的轴刻度?

fit<-lm(log(all$nq) ~ log(all$area)) 
fintercept<-fit$coefficients[[1]] #intercept 
fslope<-fit$coefficients[[2]] #slope 
fitmax<-max(fslope*log(all$area)+fintercept) 
interceptmax<-max(log(all$nq)-fslope*log(all$area)) 

ggplot(all, aes(x=log(area), y=log(nq)))+ 
    geom_point(size=1.5)+ 
    scale_color_manual(name = "Climate Classes",values=cols)+ 
    geom_abline(aes(intercept=interceptmax,slope=fslope))+ 
    theme_bw()+ 
    theme(panel.grid.major = element_line(colour = "#808080"))+ 
    theme(legend.position = "top") 

这就是我得到:enter image description here

我希望在指数标刻度线(区,并即实值NQ而不是日志值)。我怎样才能做到这一点?

另外,我想用正常值绘制和动态创建的包络曲线。但我不知道该怎么做。例如:

ggplot(all, aes(x=area, y=nq))+ 
    geom_point(size=1.5)+ 
    scale_color_manual(name = "Climate Classes",values=cols)+ 
    scale_y_log10(labels = function(y) format(y, scientific = FALSE))+ 
    scale_x_log10(labels = function(x) format(x, scientific = FALSE))+ 
    geom_abline(aes(intercept=exp(fintercept),slope=fslope))+ 
    theme_bw()+ 
    theme(panel.grid.major = element_line(colour = "#808080"))+ 
    theme(legend.position = "top") 

正如你所看到的,包络曲线是完全错误的,而且不知道如何解决。

enter image description here

数据

structure(list(area = c(1310.534, 10385.8523, 10385.8523, 7163.9071, 
1610.9726, 7163.9071, 7163.9071, 7163.9071, 1310.534, 7163.9071, 
7163.9071, 7163.9071, 1310.534, 7163.9071, 7163.9071, 1310.534, 
10385.8523, 11152.4888, 1748.242, 1748.242, 1748.242, 1748.242, 
7378.8761, 11152.4888, 11152.4888, 1748.242, 1748.242, 7378.8761, 
468.7878, 468.7878, 468.7878, 647.497, 647.497, 647.497, 647.497, 
647.497, 647.497, 647.497, 468.7878, 468.7878, 647.497, 647.497, 
647.497, 647.497, 647.497, 647.497, 468.7878, 647.497, 647.497, 
647.497, 647.497, 647.497, 647.497, 647.497, 468.7878, 647.497, 
468.7878, 468.7878, 468.7878, 647.497, 468.7878, 468.7878, 468.7878, 
468.7878, 468.7878, 468.7878, 647.497, 647.497, 647.497, 647.497, 
647.497, 468.7878, 468.7878, 647.497, 468.7878, 468.7878, 468.7878, 
647.497, 468.7878, 468.7878, 468.7878, 468.7878, 81.5846, 40.1448, 
81.5846, 40.1448, 81.5846, 81.5846, 17.4565, 19.917, 19.917, 
19.917, 19.917, 62.6777, 19.917, 19.917, 19.917, 19.917, 17.4565, 
62.6777), nq = c(0.182147887807565, 0.0460775568703206, 0.0837030197319482, 
0.0565237508454011, 0.351549678746864, 0.0837974434369759, 0.17431450220788, 
0.0612670005170782, 0.235517430299405, 0.0545473991420129, 0.0877501608026156, 
0.0624528199144291, 0.235517430299405, 0.0924934104742927, 0.0861690822316777, 
0.120783741589306, 0.0257107353625662, 0.182812378166208, 0.70134426469562, 
0.542610462395938, 0.704583690358657, 0.970219855145912, 0.212216817138317, 
0.363085690792176, 0.206425636580778, 1.17430618873131, 0.560427503743761, 
0.293956744984511, 0.156447330753915, 0.15584343278558, 0.154031312248314, 
0.122889063578673, 0.13513437127894, 0.117203786272369, 0.160062054341565, 
0.176680509716647, 0.107145361291249, 0.159624677797735, 0.233160931235838, 
0.278464371299765, 0.114142613788172, 0.108457336481868, 0.145630172803889, 
0.118515915903857, 0.136008969925729, 0.147816746641297, 0.209603364251373, 
0.124638415313121, 0.124638415313121, 0.105833386100631, 0.119390514550647, 
0.176680509716647, 0.156125974328839, 0.139944895497585, 0.205978909860709, 
0.122014464931884, 0.208395141682441, 0.172756628905445, 0.169132387830912, 
0.148691499728956, 0.18423346341351, 0.154635210216648, 0.158863562575647, 
0.16248780365018, 0.160675683112914, 0.227724569624039, 0.111081286863105, 
0.199421464500994, 0.105833386100631, 0.120265113197436, 0.110206688216316, 
0.180609222338977, 0.742974113234175, 0.150440851463404, 0.19510618663711, 
0.212019382756974, 0.244033867775569, 0.117203786272369, 0.19087783427811, 
0.202354668786176, 0.181817231591778, 0.207187132429641, 0.364440102666435, 
0.177751539427273, 0.123562535086279, 0.187628285606106, 0.336672607330305, 
0.0937125388860153, 0.110302752556354, 0.228899934729126, 0.191936536627002, 
0.174875734297334, 0.15923582868906, 0.668641319001814, 0.227479038007732, 
0.217527740121504, 0.164924436411106, 0.162077622131847, 0.139501045455847, 
0.542141782484041)), .Names = c("area", "nq"), row.names = c("100142", 
"100143", "100144", "100145", "100146", "100147", "100148", "100149", 
"100150", "100151", "100152", "100153", "100154", "100155", "100156", 
"100157", "100158", "100264", "100265", "100266", "100267", "100268", 
"100269", "100270", "100271", "100272", "100273", "100274", "100348", 
"100350", "100351", "100352", "100354", "100355", "100357", "100358", 
"100359", "100363", "100375", "100376", "100377", "100379", "100380", 
"100381", "100382", "100384", "100385", "100386", "100387", "100388", 
"100389", "100390", "100391", "100392", "100393", "100394", "100395", 
"100396", "100397", "100398", "100399", "100400", "100401", "100402", 
"100403", "100404", "100405", "100406", "100407", "100408", "100409", 
"100410", "100411", "100412", "100413", "100414", "100418", "100419", 
"100420", "100421", "100423", "100424", "100426", "100427", "100428", 
"100429", "100434", "100436", "100442", "100459", "100460", "100462", 
"100465", "100470", "100490", "100492", "100497", "100498", "100503", 
"100504"), class = "data.frame") 

回答

1

对于问题的第一部分。

aes参数中删除log()功能。在绘图之前,您正在变换变量的值,因此它显示在图中。

添加代替coord_trans(x = "log10", y = "log10")到您的情节,所以你的轴被重新调整,保持你的变量的值完好无损。

ggplot(all, aes(x = area, y = nq)) + 
    geom_point(size = 1.5) + 
    scale_color_manual(name = "Climate Classes", values = cols) + 
    geom_abline(aes(intercept = interceptmax, slope = fslope)) + 
    theme_bw() + 
    theme(panel.grid.major = element_line(colour = "#808080")) + 
    theme(legend.position = "top") + 
    coord_trans(x, = "log10", y = "log10") 

对于您的问题的secord部分。

在这种情况下,您实际上是将scale_y_log10()添加到您的情节中,因此您将重新调整数据,而您的截距不会重新调整。所以你必须在拦截的值上使用log10(),所以两者的规模都是一样的。

ggplot(all, aes(x = area, y = nq)) + 
    geom_point(size = 1.5) + 
    scale_color_manual(name = "Climate Classes", values = cols) + 
    scale_y_log10(labels = function(y) format(y, scientific = FALSE)) + 
    scale_x_log10(labels = function(x) format(x, scientific = FALSE)) + 
    geom_abline(aes(intercept = log10(exp(fintercept)), slope = fslope)) + 
    theme_bw() + 
    theme(panel.grid.major = element_line(colour = "#808080")) + 
    theme(legend.position = "top")