我正在使用ggplot2繪製大量數據框中的某些光譜數據。我想將圖形限制在400至900納米之間的波長。我知道如何使用scale_x_continuous()或xlim()來設置x軸限制。問題在於,在完成此操作後,y軸不會自動重新調整爲顯示的最小/最大值。我不想手動設置它,因爲我的循環處理的每個數據幀將具有不同的範圍。如何在R中限制X軸後自動重新計算y軸刻度ggplot2
哪裏summary
是我的數據幀的列表中,那麼這段代碼給我地塊的名單,而軸調整:
plotlist <- list()
for(i in 1:length(summary)){
plotlist[[length(plotlist)+1]] <- ggplot(summary[[i]], aes(Wavelength, average)) +
geom_line(aes(color=Sample)) +
geom_linerange(aes(ymin=average-sem, ymax=average+sem, color=Sample), alpha=0.5) +
ylab("Absorbance (AU)") + ggtitle(names(summary)[i]) +
theme_classic()
}
plotlist[[33]]
這給了我: Plot without axis adjustment
如果我調整X軸向400 - 900這樣:
plotlist <- list()
for(i in 1:length(summary)){
plotlist[[length(plotlist)+1]] <- ggplot(summary[[i]], aes(Wavelength, average)) +
scale_x_continuous(name="Wavelength (nm)", limits=c(400, 900), expand=c(0,0)) +
geom_line(aes(color=Sample), na.rm=TRUE) +
geom_linerange(aes(ymin=average-sem, ymax=average+sem, color=Sample), alpha=0.5, na.rm=TRUE) +
ylab("Absorbance (AU)") + ggtitle(names(summary)[i]) +
theme_classic()
}
plotlist[[33]]
然後我得到這個情節,與x軸正確,但y軸的規模對於顯示的數據來說現在太大了: Plot with correct x-axis, but wrong y-axis
我該如何自動調整y軸到合適的最小/最大值,但只能在x軸的400-900 nm範圍內?
編輯:我試圖改變我添加的比例,幾何等的順序,但這沒有什麼區別。 – beanstalker