繁体   English   中英

R:清理地块的增量标签和背景

[英]R: Cleaning up a plot's increment labels and background

我是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())
  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,skip = 1,stringsAsFactors = F)

    parkride $容量<-as.numeric(parkride $ Capacity)parkride $占用率<-as.numeric(parkride $ Occupancy)

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

    plot1 <-ggplot(pr_subset,aes(Capacity,Occupancy))+ geom_point(colour =“ blue”)+ scale_x_continuous(c(0,2500))+ theme(legend.title = element_blank(),legend.text = element_blank( ),legend.key = element_blank(),legend.background = element_blank())+ ggtitle(“ Puget Sound Park and Ride \\ n很多容量和入住率”)

    情节1

这很多都是通过反复试验中学到的。 尝试阅读“ R Graphics Cookbook”之类的书中的教程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM