[R] Multiple Plots in Histogram December 24, 2009
Posted by vyolian in R.Tags: boxplot, histogram, R
add a comment
Might be a misleading title but that’s what I searched for when I wanted to plot multiple series on a histogram. Looks like what I should have looked for was barplot.
t1 <- table( c(1,1,2,3,1,1,2,3,1) ) t2 <- table( c(1,2,2,3,2,2,2,2,2) ) t <- rbind(t1,t2) barplot(t, beside=TRUE)
Dataframes in R (basic cheatsheet) December 17, 2009
Posted by vyolian in R.add a comment
Construct
#construct and initialize dataframe df <- data.frame(x=10:15, y=c(2,4,6,1,3,0))
#construct empty dataframe df <- data.frame(x=numeric(0), y=numeric(0)) #add new row df <- rbind(df, data.frame(x=16, y=-1))
Get/Set
column_names <- colnames(df) ys <- df$y y_row2 <- df$y[2] df$y[2] = 12
range_xs <- range(df$x) range_xs_and_ys <- range( c(df$x, df$y) ) num_rows <- nrow(df)
#select certain rows subset_xs <- df[ (df$x > 11 & df$y < 6), ] subset_xs <- df[ with(df, x > 11 & y < 6), ] subset_xs <- subset(df, x > 11 & y < 6) #select certain columns subset_ys <- subset(df, select=c(x,z)) subset_ys <- subset(df, select=-c(y))
Manipulate
ordered <- df[ order(df$y), ]
Transform
colnames(df) = c("rename_x", "rename_y")
df <- transform(df, new_z=(x+y))
#add column
df <- merge(df, list(c=15:20), by=0, all.x=TRUE)
Plotting Week Days in R December 13, 2009
Posted by vyolian in R.add a comment
Say you’re plotting a graph with week days vs frequency but your week days is in the form of numbers (Sunday is 0, Monday is 1, etc). Here’s how you label the x axis with the right tick marks so that Sunday is ‘Sun’, Monday is ‘Mon’, etc.
day_of_week <- c(0, 1, 2, 3, 4, 5, 6)
frequency <- c(10, 15, 2, 9, 15, 16, 7)
plot(day_of_week, frequency, xaxt="n")
axis(1, at=day_of_week, labels=c('Sun','Mon','Tues','Wed','Thurs','Fri','Sat'))