facet_wrap.R

ShimadaYoshio — Oct 23, 2013, 11:12 PM


library(ggplot2)
Warning: package 'ggplot2' was built under R version 3.0.2
qplot(wt, mpg, data = mtcars, label = rownames(mtcars), size = wt) +
  geom_text(colour = "red")

plot of chunk unnamed-chunk-1


p <- qplot(wt, mpg, data = mtcars, ###!!!!
           size=as.factor(gear),
           colour=as.factor(carb),
           label = rownames(mtcars), size = cyl) +
  geom_text(colour = 2,hjust=.5, vjust=.2) +
  geom_hline(yintercept = mean(mtcars$mpg),colour="blue", 
             linetype = "longdash",size=.2) +
  geom_vline(xintercept = mean(mtcars$wt),colour="blue", 
             linetype = "longdash",size=.2)
p 

plot of chunk unnamed-chunk-1

p + facet_wrap(vs ~ am , scales = "free")

plot of chunk unnamed-chunk-1

p + facet_wrap(vs ~ am , scales = "free_x")

plot of chunk unnamed-chunk-1

p + facet_wrap(vs ~ am , scales = "free_y")

plot of chunk unnamed-chunk-1


p + facet_wrap( ~ am ,nrow=2,ncol=2)

plot of chunk unnamed-chunk-1

p + facet_grid( vs ~ am )

plot of chunk unnamed-chunk-1


p + geom_smooth()
geom_smooth: method="auto" and size of largest group is <1000, so using
loess. Use 'method = x' to change the smoothing method.
Warning: span too small.  fewer data values than degrees of freedom.
Warning: pseudoinverse used at 2.46 Warning: neighborhood radius 0.75497
Warning: reciprocal condition number 0 Warning: There are other near
singularities as well. 0.062488 Warning: span too small.  fewer data
values than degrees of freedom. Warning: pseudoinverse used at 2.46
Warning: neighborhood radius 0.75497 Warning: reciprocal condition number
0 Warning: There are other near singularities as well. 0.062488 Warning:
span too small.  fewer data values than degrees of freedom. Warning:
pseudoinverse used at 1.8326 Warning: neighborhood radius 0.36743 Warning:
reciprocal condition number 0 Warning: There are other near singularities
as well. 0.1501 Warning: span too small.  fewer data values than degrees
of freedom. Warning: pseudoinverse used at 1.8326 Warning: neighborhood
radius 0.36743 Warning: reciprocal condition number 0 Warning: There are
other near singularities as well. 0.1501 Warning: span too small.  fewer
data values than degrees of freedom. Warning: pseudoinverse used at 3.4329
Warning: neighborhood radius 0.08705 Warning: reciprocal condition number
0 Warning: There are other near singularities as well. 0.16569 Warning:
span too small.  fewer data values than degrees of freedom. Warning:
pseudoinverse used at 3.4329 Warning: neighborhood radius 0.08705 Warning:
reciprocal condition number 0 Warning: There are other near singularities
as well. 0.16569 Warning: span too small.  fewer data values than degrees
of freedom. Warning: pseudoinverse used at 1.6071 Warning: neighborhood
radius 1.5429 Warning: reciprocal condition number 0 Warning: There are
other near singularities as well. 0.17462 Warning: span too small.  fewer
data values than degrees of freedom. Warning: pseudoinverse used at 1.6071
Warning: neighborhood radius 1.5429 Warning: reciprocal condition number 0
Warning: There are other near singularities as well. 0.17462 Warning: span
too small.  fewer data values than degrees of freedom. Warning: at 1.5099
Warning: radius 9.8282e-006 Warning: all data on boundary of neighborhood.
make span bigger Warning: pseudoinverse used at 1.5099 Warning:
neighborhood radius 0.003135 Warning: reciprocal condition number 1
Warning: at 2.1431 Warning: radius 9.8282e-006 Warning: all data on
boundary of neighborhood. make span bigger Warning: There are other near
singularities as well. 9.8282e-006 Warning: zero-width neighborhood. make
span bigger Warning: zero-width neighborhood. make span bigger
Error: argument is of length zero
#################################################
data(cars)
head(cars,5)
  speed dist
1     4    2
2     4   10
3     7    4
4     7   22
5     8   16

plot(cars$speed,cars$dist)

plot of chunk unnamed-chunk-1


library(ggplot2)
library(gridExtra)
Loading required package: grid

p <- qplot(speed,dist,data=cars)
p1 <- p + geom_point()
p2 <- p1 + geom_smooth()
p3 <- p + geom_density2d()
p4 <- p1 + geom_density2d()
grid.arrange(p1,p2,p3,p4,ncol=2)
geom_smooth: method="auto" and size of largest group is <1000, so using
loess. Use 'method = x' to change the smoothing method.

plot of chunk unnamed-chunk-1


ggplot() +
  geom_point(
    aes(x = speed,y = dist),
    data = cars
    )

plot of chunk unnamed-chunk-1


ggplot() +
  geom_density2d(
    aes(x = speed,y = dist),
    data = cars
    ) +
  geom_point(
    aes(x = speed,y = dist),
    data = cars
  ) 

plot of chunk unnamed-chunk-1



#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#only violent crimes
violent_crimes <- subset(crime,
                         offense != 'auto theft' &
                         offense != 'theft' &
                         offense != 'burglary'
)
Error: object 'crime' not found

#rank violent crimes
violent_crimes$offense <-
  factor(violent_crimes$offense,
         levels = c('robbery','aggravated assault',
                    'rape','murder')
)
Error: object 'violent_crimes' not found

#restrict to downtown
violent_crimes <- subset(violent_crimes,
          -95.39681 <= lon & lon <= -95.34188 &
           29.73631 <= lat & lat <= 29.78400
)
Error: object 'violent_crimes' not found
library(ggmap)
HoustonMap <- qmap('houston',zoom = 14,
                   color = 'bw',legend = 'topleft')
Map from URL :
http://maps.googleapis.com/maps/api/staticmap?center=houston&zoom=14&size=%20640x640&scale=%202&maptype=terrain&sensor=false
Google Maps API Terms of Service : http://developers.google.com/maps/terms
Information from URL :
http://maps.googleapis.com/maps/api/geocode/json?address=houston&sensor=false
Google Maps API Terms of Service : http://developers.google.com/maps/terms
HoustonMap 

plot of chunk unnamed-chunk-1

HoustonMap +
  geom_point(
    aes(x = lon, y = lat,size = offense,
        colour = offense),
    data = violent_crimes
    )
Error: object 'violent_crimes' not found

HoustonMap +
  stat_bin2d(
    aes(x = lon, y = lat,
        colour = offense,fill = offense),
    size = .5,bins = 30,alpha = 1/2,
    data = violent_crimes
)
Error: object 'violent_crimes' not found

houston <- get_map('houston',zoom = 14)
Map from URL :
http://maps.googleapis.com/maps/api/staticmap?center=houston&zoom=14&size=%20640x640&scale=%202&maptype=terrain&sensor=false
Google Maps API Terms of Service : http://developers.google.com/maps/terms
Information from URL :
http://maps.googleapis.com/maps/api/geocode/json?address=houston&sensor=false
Google Maps API Terms of Service : http://developers.google.com/maps/terms
HoustonMap <- ggmap(houston,
                    extent = 'device',legend = 'topleft')
HoustonMap +
  stat_density2d(
    aes(x = lon,y = lat,
        fill = ..level..,alpha = ..level..),
    size = 2,bins = 4,
    data = violent_crimes,
    geom = 'polygon'
)
Error: object 'violent_crimes' not found

# using the 'inset' function...

geocode('vanderbilt university')
Information from URL :
http://maps.googleapis.com/maps/api/geocode/json?address=vanderbilt+university&sensor=false
Google Maps API Terms of Service : http://developers.google.com/maps/terms
    lon   lat
1 -86.8 36.14

mapdist('baylor university, waco',
        'vanderbilt university')
Information from URL :
http://maps.googleapis.com/maps/api/distancematrix/json?origins=baylor+university+waco&destinations=vanderbilt+university&mode=driving&sensor=false
Google Maps API Terms of Service : http://developers.google.com/maps/terms
                     from                    to       m   km miles seconds
1 baylor university, waco vanderbilt university 1217798 1218 756.7   39120
  minutes hours
1     652 10.87

SS <- geocode('Japan Sapporo')
Information from URL :
http://maps.googleapis.com/maps/api/geocode/json?address=Japan+Sapporo&sensor=false
Google Maps API Terms of Service : http://developers.google.com/maps/terms
sapporo <- get_map('SS',zoom = 14)
Map from URL :
http://maps.googleapis.com/maps/api/staticmap?center=SS&zoom=14&size=%20640x640&scale=%202&maptype=terrain&sensor=false
Google Maps API Terms of Service : http://developers.google.com/maps/terms
Information from URL :
http://maps.googleapis.com/maps/api/geocode/json?address=SS&sensor=false
Google Maps API Terms of Service : http://developers.google.com/maps/terms
sapporonMap <- ggmap(sapporo,
                    extent = 'device',legend = 'topleft')
sapporonMap

plot of chunk unnamed-chunk-1

#-----------------------------------------
library(grid)
HoustonMap +
  geom_point(aes(x = lon, y = lat, colour = offense, 
                 size = offense), data = violent_crimes) +
  scale_colour_discrete('Offense', 
              labels = c('Robery','Aggravated Assault',
                         'Rape','Murder')) +
  scale_size_discrete('Offense', 
                labels = c('Robery','Aggravated Assault',
                           'Rape','Murder'),
                      range = c(1.75,6)) +
  guides(size = guide_legend(override.aes = list(size = 6))) +
  opts(
    legend.key.size = unit(1.8,'lines'),
    legend.title = theme_text(size = 16, face = 'bold'),
    legend.text = theme_text(size = 14)
  ) +
  labs(colour = 'Offense', size = 'Offense')
Error: object 'violent_crimes' not found

#------------------------------------------
HoustonMap +
  stat_bin2d(aes(x = lon, y = lat, colour = offense, fill = offense),
             size = .5, bins = 30, alpha = 1/2, 
             data = violent_crimes) +
  scale_colour_discrete('Offense',
                        labels = c('Robery','Aggravated Assault',
                                   'Rape','Murder'),
                        guide = FALSE) +
  scale_fill_discrete('Offense', labels = c('Robery',
                                    'Aggravated Assault',
                                    'Rape','Murder')) +
  opts(
    legend.text = theme_text(size = 15, vjust = .5),
    legend.title = theme_text(size = 15,face='bold'),
    legend.key.size = unit(1.8,'lines')
  )
Error: object 'violent_crimes' not found

#-------------------------------------
HoustonMap +
  stat_density2d(aes(x = lon, y = lat, fill = ..level.., 
                     alpha = ..level..),
                 size = 2, bins = 4, 
                 data = violent_crimes, geom = 'polygon') +
  scale_fill_gradient('Violent\nCrime\nDensity') +
  scale_alpha(range = c(.4, .75), guide = FALSE) +
  guides(fill = guide_colorbar(barwidth = 1.5, 
                               barheight = 10))
Error: object 'violent_crimes' not found


##############################################
##############################################
library(ggplot2)

xy <- data.frame(x = 1:10, y = 10:1)

plot <- ggplot(data = xy) + geom_point(aes(x = x, y = y))
plot <- plot + theme(plot.background = 
                       element_rect(fill = "transparent", colour = "transparent"))
plot <- plot + theme(axis.text = element_text(colour = 'white', size = 15))
plot <- plot + theme(axis.ticks = element_line(colour = 'white'))
plot <- plot + theme(axis.title = element_text(colour = 'white', size = 20))

png('testGraph1.png', width = 300, height = 300, units = "px", 
    bg = "transparent")
print(plot)
dev.off()
pdf 
  2 

library(ggplot2)
library(reshape)
Warning: package 'reshape' was built under R version 3.0.1
Loading required package: plyr

Attaching package: 'reshape'

The following object is masked from 'package:plyr':

rename, round_any

df <- structure(
  c(1084, 888, 740, 646, 567, 513, 466,
    5982, 5266, 4607, 4050, 3625, 3216, 2876,
    3605, 3748, 3791, 3708, 3517, 3299, 3019),
  .Dim = c(7, 3),
  .Dimnames = list(c("2010", "2015", "2020", 
                     "2025", "2030", "2035", "2040"),
                   c("0~14", "15~64", "65~")))

df.m <- melt(df)
df.m <- rename(df.m, c(X1 = "Year", X2 = "age"))

g <- ggplot(df.m, aes(x = Year, y = value, fill = age)) + 
  ggtitle("Forecast of Population in Otaki Town (2010-2040)") +
  labs(x = NULL, y = "Number of People ", fill = NULL)
g <- g + geom_bar(stat = "identity", position = "stack")
g <- g + scale_fill_brewer(palette = "Set1")
print(g)

plot of chunk unnamed-chunk-1



#=========================================
library(ggplot2)
library(gridExtra)

con <- url("http://ppl.ug/pkMK8YBIQ2k/japan_meat_df.RData")
load(con)
Warning: cannot open: HTTP status was '403 Forbidden'
Error: cannot open the connection

beef <- ggplot(japan2.df, aes(long, lat, group = group, fill = meat.Beef))
Error: object 'japan2.df' not found
beef <- beef + geom_polygon()
Error: object 'beef' not found
beef <- beef + scale_fill_manual(values = c("gray95", "cyan"))
Error: object 'beef' not found
beef <- beef + geom_path(color = "black", size = 0.1) +
  coord_equal() + xlim(c(125, 150)) + labs(x = "", y = "") +
  theme(axis.ticks = element_blank(), 
        axis.text = element_blank()) +
NANA
Error: object 'beef' not found

pork <- ggplot(japan2.df, aes(long, lat, group = group, fill = meat.Pork))
Error: object 'japan2.df' not found
pork <- pork + geom_polygon()
Error: object 'pork' not found
pork <- pork + scale_fill_manual(values = c("gray95", "green"))
Error: object 'pork' not found
pork <- pork + geom_path(color = "black", size = 0.1) +
  coord_equal() + xlim(c(125, 150)) + labs(x = "", y = "") +
  theme(axis.ticks = element_blank(), 
        axis.text = element_blank()) +
NANA
Error: object 'pork' not found

grid.arrange(beef, pork, ncol = 2)
Error: object 'beef' not found