我正在尝试绘制包络线:
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")
我希望指数刻度上的刻度线(即面积和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")
正如您所看到的,包络曲线完全错误,并且不确定如何修复。
数据
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")
答案 0 :(得分:1)
问题的第一部分。
从log()
参数中删除aes
函数。在绘图之前,您正在变换变量的值,因此它就是在图中显示的。
将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")
对于你问题的第二部分。
在这种情况下,您实际上是在为您的绘图添加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")