2013-10-23 35 views
0

我是R的新手,這是一項家庭作業。我只需要創建一些簡單的圖。R:清理繪圖的增量標籤和背景

我需要通過給每個軸添加0,500,1000,1500,2000和2500來清理繪圖的增量標籤(我不知道還有什麼可以稱它們的)。我還想將背景上的間距減少到100的間隔。另外,如果您知道更簡單的方法來刪除所有非常酷的圖例元素。謝謝您的幫助。

這是我的代碼。我假設你可以複製並粘貼到你的計算機上,看看我得到的混亂結果。

library("ggplot2") 
library("lubridate") 
library("reshape2") 
library("gdata") 

# PSRC park and ride data 

parkride <- read.xls("http://www.psrc.org/assets/5748/parkandride-2010.xls", 
       sheet=2, 
       na.strings="*", 
       skip=1) 

plot1 <- ggplot(parkride, 
      aes(x=Capacity, y=Occupancy, color="blue")) + 
    geom_point() + 
    xlim=c(0, 2500), ylim=c(0, 2500) + 
    theme(axis.title.y=element_text(angle=0)) + 
    ggtitle("Puget Sound Park and Ride \nLots Capacity and Occupancy") + 
    theme(plot.title=element_text(face="bold", lineheight=1.25)) + 
    scale_fill_discrete(guide=FALSE) + 
    theme(legend.title=element_blank()) + 
    theme(legend.text=element_blank()) + 
    theme(legend.key=element_blank()) + 
    theme(legend.background=element_blank()) 
+0

我收到錯誤消息,提示您未包含所有數據操作。例如,parkride $容量是一個因素,其他大多數欄目也是如此。 –

+0

由於這*是作業,所以重要的是要學習如何使用'ggplot2'。我很容易在網上找到兩個帶有「ggplot-intro.pdf」標題的PDF文件;這些應該對你非常有用。 –

回答

0
  1. 添加stringsAsFactors=F當你在數據讀取。通常應該這樣做,除非你有理由不這樣做。
  2. 檢查str(parkride)並注意您的號碼以字符形式出現。您可能需要清理數據,因爲並非每個值都是數字。
  3. 重新編碼將顯示爲數字的列
  4. 子集可刪除NA或手動清理數據。
  5. 在geom_point中添加顏色
  6. 組合主題一起變化。
  7. 使用xlim的長格式scale_x_continous之前由於因素和字符而無法使用。

    parkride < - read.xls( 「http://www.psrc.org/assets/5748/parkandride-2010.xls」,2, 跳過= 1,stringsAsFactors = F)

    parkride $容量< - as.numeric(parkride $容量) parkride $佔有< - 如.nu​​meric(parkride $入住)

    pr_subset < - 子集(!parkride,is.na(容量)& is.na(入住))

    plot1 < - ggplot(pr_subset,AES(CA pacity,佔有))+ geom_point(顏色= 「藍」)+ scale_x_continuous(C(0,2500))+ 主題( legend.title = element_blank(), legend.text = element_blank(), 圖例.KEY = element_blank(), legend.background = element_blank() )+ ggtitle( 「普吉特停車換乘\ nLots容量和佔用」)

    plot1

很多這通過反覆試驗來了解。嘗試閱讀「R Graphics Cookbook」等書籍中的教程。