Search Results

Search found 65 results on 3 pages for 'ggplot'.

Page 1/3 | 1 2 3  | Next Page >

  • Using ggplot in R

    - by g256
    I have a time series cvs file like this (in var there are the values): Hour,Date,Type,val1,val2,val3,val4,val5,val6..... 0100,0153,aaa, 0100,0153,bbb, 0100,0153,ccc, 0100,0153,ddd, 0200,0153,aaa, 0200,0153,bbb, 0200,0153,ccc, 0200,0153,ddd, . . 0100,0154,aaa, 0100,0154,bbb, 0100,0154,ccc, 0100,0154,ddd, . . I would like to use ggplot to plot the barplot mean of the time series of val1,val2,... for each type. One plot for each type except for ccc and ddd that should be considered as one type, so for this I should first sum up the value, then take the mean, then plot). So three plot at the end. Thanks for advice.

    Read the article

  • ggplot: showing % instead of counts in charts of categorical variables

    - by wishihadabettername
    I'm plotting a categorical variable and instead of showing the counts for each category value, I'm looking for a way to get ggplot to display the percentage of values in that category. Of course, it is possible to create another variable with the calculated percentage and plot that one, but I have to do it several dozens of times and I hope to achieve that in one command. I was experimenting with something like qplot (mydataf) + stat_bin(aes(n=nrow(mydataf), y=..count../n)) + scale_y_continuous(formatter="percent") but I must be using it incorrectly, as I got errors. To easily reproduce the setup, here's a simplified example: mydata <- c ("aa", "bb", null, "bb", "cc", "aa", "aa", "aa", "ee", null, "cc"); mydataf <- factor(mydata); qplot (mydataf); #this shows the count, I'm looking to see % displayed. In the real case I'll probably use ggplot instead of qplot, but the right way to use stat_bin still eludes me. Thank you. UPDATE: I've also tried these four approaches: ggplot(mydataf, aes(y = (..count..)/sum(..count..))) + scale_y_continuous(formatter = 'percent'); ggplot(mydataf, aes(y = (..count..)/sum(..count..))) + scale_y_continuous(formatter = 'percent') + geom_bar(); ggplot(mydataf, aes(x = levels(mydataf), y = (..count..)/sum(..count..))) + scale_y_continuous(formatter = 'percent'); ggplot(mydataf, aes(x = levels(mydataf), y = (..count..)/sum(..count..))) + scale_y_continuous(formatter = 'percent') + geom_bar(); but all 4 give: Error: ggplot2 doesn't know how to deal with data of class factor The same error appears for the simple case of ggplot (data=mydataf, aes(levels(mydataf))) + geom_bar() so it's clearly something about how ggplot interacts with a single vector. I'm scratching my head, googling for that error gives a single result.

    Read the article

  • ggplot add percentage labels based on x-axis variables

    - by eugeneyan
    I've a ggplot that shows the counts of tweets for some brands as well as a label for the overall percentage. This was done with much help from this link: ggplot: showing % instead of counts in charts of categorical variables # plot ggplot of brands ggplot(data = test, aes(x = brand, fill = brand)) + geom_bar() + stat_bin(aes(label = sprintf("%.02f %%", ..count../sum(..count..)*100)), geom = 'text', vjust = -0.3) Next, I would like to plot it based on brand and sentiment, with the labels for the bars of each brand totalling up to 100%. However, I have difficulty amending my code to do this. Would you be able to help please? Also, would it be possible to change the colours for neu to blue and pos to green? # plot ggplot of brands and sentiment ggplot(data = test, aes(x = brand, fill = factor(sentiment))) + geom_bar(position = 'dodge') + stat_bin(aes(label = sprintf("%.02f %%", ..count../sum(..count..)*100)), geom = 'text', position = position_dodge(width = 0.9), vjust=-0.3)

    Read the article

  • for (i in xxx) ggplot problem

    - by Andreas
    This is strange - I think? library(ggplot2) tf <- which(sapply(diamonds, is.factor)) diamonds.tf <- diamonds[,tf] So far so good. But next comes the trouble: pl.f <- ggplot(diamonds.tf, aes(x=diamonds.tf[,i]))+ geom_bar()+ xlab(names(diamonds.tf[i])) for (i in 1:ncol(diamonds.tf)) { ggsave(paste("plot.f",i,".png",sep=""), plot=pl.f, height=3.5, width=5.5) } This saves the plots in my working directory - but with the wrong x-label. I think this is strange since calling ggplot directly produces the right plot: i <- 2 ggplot(diamonds, aes(x=diamonds[,i]))+geom_bar()+xlab(names(diamonds)[i]) I don't really know how to describe this as a fitting title - suggestions as to a more descriptive question-title is most welcome. Thanks in advance

    Read the article

  • Fractional y-var in ggplot

    - by Misha
    How can I easily create a fractional y-value when using ggplot? t <- as.factor(test=sample(seq(0,100,10),1000,rep=T)) d <- as.factor(sample(c(0,1),1000,rep=T) p <- data.frame(t,d) My best shot was: ggplot(p,aes(x=t,y=prop.table(table(t,d),1)[,1])) + geom_point() However this doesnt work and I guess there is an easier way around this...

    Read the article

  • how to change strip.text labels in ggplot with facet and margin=TRUE

    - by Andreas
    I have looked here but still can't figure it out. How do I change the strip.text.x labels in a ggplot with faceting? Specifically I am using facet_grid with margins. The strip.text label for the margin is "(all)" - but since I am in a non-english speaking country I would rather write "Total" or something similar in my native tongue. opts(stip.text.x=c(levels(facetvariabel,"Total")) does not work. Any ideas? Example (not really the best dataset for this - but I guess it will work) ggplot(cars, aes(x=dist))+geom_bar()+facet_grid(.~speed, margin=T)

    Read the article

  • Changing text size on a ggplot bump plot

    - by Tom Liptrot
    Hi, I'm fairly new to ggplot. I have made a bumpplot using code posted below. I got the code from someones blog - i've lost the link.... I want to be able to increase the size of the lables (here letters which care very small on the left and right of the plot) without affecting the width of the lines (this will only really make sense after you have run the code) I have tried changing the size paramater but that always alter the line width as well. Any suggestion appreciated. Tom require(ggplot2) df<-matrix(rnorm(520), 5, 10) #makes a random example colnames(df) <- letters[1:10] Price.Rank<-t(apply(df, 1, rank)) dfm<-melt(Price.Rank) names(dfm)<-c( "Date","Brand", "value") p <- ggplot(dfm, aes(factor(Date), value, group = Brand, colour = Brand, label = Brand)) p1 <- p + geom_line(aes(size=2.2, alpha=0.7)) + geom_text(data = subset(dfm, Date == 1), aes(x = Date , size =2.2, hjust = 1, vjust=0)) + geom_text(data = subset(dfm, Date == 5), aes(x = Date , size =2.2, hjust = 0, vjust=0))+ theme_bw() + opts(legend.position = "none", panel.border = theme_blank()) p1 + theme_bw() + opts(legend.position = "none", panel.border = theme_blank())

    Read the article

  • Utilise Surv object in ggplot or lattice

    - by Misha
    Anyone know how to take advantage of ggplot or lattice in doing survival analysis? It would be nice to do trellis/facet like survival graphs. So in the end I played around and sort of found a solution for a kaplan meier plot. Apologize for the messy code in taking the list elements into a dataframe, but I couldnt figure out another way. Note: It only works with two levels of stratum. If anyone know how I can use x<-length(stratum) to do this please let me know (in stata I could append to a macro-unsure how this works in R)... ggkm<-function(time,event,stratum) { m2s<-Surv(time,as.numeric(event)) fit <- survfit(m2s ~ stratum) f$time<-fit$time f$surv<-fit$surv f$strata<-c(rep(names(fit$strata[1]),fit$strata[1]),rep(names(fit$strata[2]),fit$strata[2])) f$upper<-fit$upper f$lower<-fit$lower r<-ggplot (f,aes(x=time,y=surv,fill=strata,group=strata))+geom_line()+geom_ribbon(aes(ymin=lower,ymax=upper),alpha=0.3) return(r) }

    Read the article

  • Issue with plotting daily data using ggplot

    - by user1723765
    I tried to plot daily data from 9 variables in ggplot, but the graph I get cannot handle the date variable properly. The x axis is unreadable and its impossible to read the plot. I'm guessing there's an issue with the handling of dates. Here's the data: https://dl.dropbox.com/u/22681355/su.csv Here's the code I've been using: su=read.csv(file="su.csv", head=TRUE) meltdf=melt(su) ggplot(meltdf, aes(x=Date, y=value, colour=variable, group=variable))+geom_line() and here's the output: https://dl.dropbox.com/u/22681355/output.jpg here's the same plot done in excel, why does it look completely different?

    Read the article

  • How to customize notches in ggplot boxplot

    - by cjy8709
    I had a question on how to change/customize the upper and lower limit of a notch on a boxplot created by ggplot2. I looked through the function stat_boxplot and found that ggplot calculates the notch limits with the equation median +/- 1.58 * iqr / sqrt(n). However instead of that equation I wanted to change it with my own set of upper and lower notch limits. My data has 4 factors and for each factor I calculated the median and did a bootstrap to get a 95% confidence interval of that median. Thus in the end I would like to change every boxplot to have its own unique notch upper and lower limit. I'm not sure if this is even possible in ggplot and was wondering if people have an idea on how to do this? Thanks again!

    Read the article

  • Change the colour of ablines on ggplot

    - by Sarah
    Using this data I am fitting a plot: p <- ggplot(dat, aes(x=log(Explan), y=Response)) + geom_point(aes(group=Area, colour=Area))+ geom_abline(slope=-0.062712, intercept=0.165886)+ geom_abline(slope= -0.052300, intercept=-0.038691)+ scale_x_continuous("log(Mass) (g)")+ theme(axis.title.y=element_text(size=rel(1.2),vjust=0.2), axis.title.x=element_text(size=rel(1.2),vjust=0.2), axis.text.x=element_text(size=rel(1.3)), axis.text.y=element_text(size=rel(1.3)), text = element_text(size=13)) + scale_colour_brewer(palette="Set1") The two ablines represent the phylogenetically adjusted relationships for each Area trend. I am wondering, is it possible to get the ablines in the same colour palette as their appropriate area data? The first specified is for Area A, the second for Area B. I used: g <- ggplot_build(p) to find out that the first colour is #E41A1C and the second is #377EB8, however when I try to use aes within the +geom_abline command to specify these colours i.e. p <- ggplot(dat, aes(x=log(Explan), y=Response)) + geom_point(aes(group=Area, colour=Area))+ geom_abline(slope=-0.062712, intercept=0.165886,aes(colour='#E41A1C'))+ geom_abline(slope= -0.052300, intercept=-0.038691,aes(colour=#377EB8))+ scale_x_continuous("log(Mass) (g)")+ theme(axis.title.y=element_text(size=rel(1.2),vjust=0.2), axis.title.x=element_text(size=rel(1.2),vjust=0.2), axis.text.x=element_text(size=rel(1.3)), axis.text.y=element_text(size=rel(1.3)), text = element_text(size=13)) + scale_colour_brewer(palette="Set1") It changes the colour of the points and adds to the legend, which I don't want to do. Any advice would be much appreciated!

    Read the article

  • Return call from ggplot object

    - by aL3xa
    I've been using ggplot2 for a while now, and I can't find a way to get formula from ggplot object. Though I can get basic info with summary(<ggplot_object>), in order to get complete formula, usually I was combing up and down through .Rhistory file. And this becomes frustrating when you experiment with new graphs, especially when code gets a bit lengthy... so searching through history file isn't quite convenient way of doing this... Is there a more efficient way of doing this? Just an illustration: p <- qplot(data = mtcars, x = factor(cyl), geom = "bar", fill = factor(cyl)) + scale_fill_manual(name = "Cylinders", value = c("firebrick3", "gold2", "chartreuse3")) + stat_bin(aes(label = ..count..), vjust = -0.2, geom = "text", position = "identity") + xlab("# of cylinders") + ylab("Frequency") + opts(title = "Barplot: # of cylinders") I can get some basic info with summary: > summary(p) data: mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb [32x11] mapping: fill = factor(cyl), x = factor(cyl) scales: fill faceting: facet_grid(. ~ ., FALSE) ----------------------------------- geom_bar: stat_bin: position_stack: (width = NULL, height = NULL) mapping: label = ..count.. geom_text: vjust = -0.2 stat_bin: width = 0.9, drop = TRUE, right = TRUE position_identity: (width = NULL, height = NULL) But I want to get code I typed in to get the graph. I reckon that I'm missing something essential here... it's seems impossible that there's no way to get call from ggplot object!

    Read the article

  • R: Creating Custom Shapes with ggplot

    - by Brandon Bertelsen
    Full Disclosure: This was also posted to the ggplot2 mailing list. (I'll update if I receive a response) I'm a bit lost on this one, I've tried messing around with geom_polygon but successive attempts seem worse than the previous. The image that I'm trying to recreate is this, the colours are unimportant, but the positions are: In addition to creating this, I also need to be able to label each element with text. At this point, I'm not expecting a solution (although that would be ideal) but pointers or similar examples would be immensely helpful. One option that I played with was hacking scale_shape and using 1,1 as coords. But was stuck with being able to add labels. The reason I'm doing this with ggplot, is because I'm generating scorecards on a company by company basis. This is only one plot in a 4 x 10 grid of other plots (using pushViewport) Note: The top tier of the pyramid could also be a rectangle of similar size.

    Read the article

  • How can I use splne() with ggplot?

    - by David
    I would like to fit my data using spline(y~x) but all of the examples that I can find use a spline with smoothing, e.g. lm(y~ns(x), df=_). I want to use spline() specifically because I am using this to do the analysis represented by the plot that I am making. Is there a simple way to use spline() in ggplot? I have considered the hackish approach of fitting a line using geom_smooth(aes(x=(spline(y~x)$x, y=spline(y~x)$y)) but I would prefer not to have to resort to this. Thanks!

    Read the article

  • Margin adjustments when using ggplot's geom_tile()

    - by chris_dubois
    From the documentation for ggplot2's geom_tile() function, we have the following simple plot: > # Generate data > pp <- function (n,r=4) { + x <- seq(-r*pi, r*pi, len=n) + df <- expand.grid(x=x, y=x) + df$r <- sqrt(df$x^2 + df$y^2) + df$z <- cos(df$r^2)*exp(-df$r/6) + df + } > p <- ggplot(pp(20), aes(x=x,y=y)) > > p + geom_tile() How do I remove the margins that border the tile?

    Read the article

  • how to define fill colours in ggplot histogram?

    - by Andreas
    I have the following simple data data <- structure(list(status = c(9, 5, 9, 10, 11, 10, 8, 6, 6, 7, 10, 10, 7, 11, 11, 7, NA, 9, 11, 9, 10, 8, 9, 10, 7, 11, 9, 10, 9, 9, 8, 9, 11, 9, 11, 7, 8, 6, 11, 10, 9, 11, 11, 10, 11, 10, 9, 11, 7, 8, 8, 9, 4, 11, 11, 8, 7, 7, 11, 11, 11, 6, 7, 11, 6, 10, 10, 9, 10, 10, 8, 8, 10, 4, 8, 5, 8, 7), statusgruppe = c(0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, NA, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0)), .Names = c("status", "statusgruppe"), class = "data.frame", row.names = c(NA, -78L )) from that I'd like to make a histogram: ggplot(data, aes(status))+ geom_histogram(aes(y=..density..), binwidth=1, colour = "black", fill="white")+ theme_bw()+ scale_x_continuous("Staus", breaks=c(min(data$status,na.rm=T), median(data$status, na.rm=T), max(data$status, na.rm=T)),labels=c("Low", "Middle", "High"))+ scale_y_continuous("Percent", formatter="percent") Now - i'd like for the bins to take colou according to value - e.g. bins with value 9 gets dark grey - everything else should be light grey. I have tried with "fill=statusgruppe", scale_fill_grey(breaks=9) etc. - but I can't get it to work. Any ideas?

    Read the article

  • How to manually add a legend to a ggplot object

    - by Dan
    I have this data frame: structure(list(month_num = 1:24, founded_month = c(4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L), founded_year = c(2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2010L, 2010L, 2010L), count = c(270L, 222L, 256L, 250L, 277L, 268L, 246L, 214L, 167L, 408L, 201L, 225L, 203L, 220L, 230L, 225L, 177L, 207L, 166L, 135L, 116L, 122L, 69L, 42L), month_abb = c("Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar"), short_year = c("08", "08", "08", "08", "08", "08", "08", "08", "08", "09", "09", "09", "09", "09", "09", "09", "09", "09", "09", "09", "09", "10", "10", "10" ), proj = c(282, 246, 292, 298, 337, 340, 330, 310, 275, 528, 333, 369, 359, 388, 410, 417, 381, 423, 394, 375, 368, 386, 345, 330), label = c("Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan\n09", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan\n10", "Feb", "Mar")), .Names = c("month_num", "founded_month", "founded_year", "count", "month_abb", "short_year", "proj", "label"), row.names = c(NA, -24L), class = "data.frame") and i've got all of this done (I know the code's a bit ugly looking, pointers appreciated): p <- ggplot(m_summary2, aes(x = month_num, y = count)) p + geom_line(colour = rgb(0/255, 172/255, 0/255)) + geom_point(colour = rgb(0/255, 172/255, 0/255)) + geom_line(aes(x = m_summary2$month_num, y = m_summary2$proj), colour = rgb(18/255, 111/255, 150/255)) + geom_point(aes(x = m_summary2$month_num, y = m_summary2$proj), colour = rgb(18/255, 111/255, 150/255)) + scale_x_continuous("Month", breaks = m_summary2$month_num, labels = m_summary2$label) + scale_y_continuous("# Startups Founded") + opts(title = paste("# Startups Founded:", m_summary2$month_abb[1], m_summary2$short_year[1], "-", m_summary2$month_abb[nrow(m_summary2)], m_summary2$short_year[nrow(m_summary2)])) Now I would like to add a legend to clarify that the blue line is a projection and the green line is the current data. Thanks in advance!

    Read the article

  • ggplot geom_bar - to many bars

    - by Andreas
    I am sorry for the non-informative title. exstatus <- structure(list(org = structure(c(2L, 1L, 7L, 3L, 6L, 2L, 2L, 7L, 2L, 1L, 2L, 2L, 4L, 2L, 2L, 2L, 2L,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L, 2L, 5L, 2L, 5L, 2L, 5L, 2L, 3L, 2L, 5L, 2L, 3L, 4L, 2L, 3L, 3L, 2L, 5L, 3L, 4L, 4L, 1L, 2L, 2L, 2L, 5L, 5L, 5L, 5L, 2L, 2L, 2L, 2L, 5L, 2L, 3L, 5L, 5L, 4L, 2L, 5L, 2L, 2L, 2L, 2L, 2L, 5L, 5L, 2L, 2L, 5L, 2L, 5L, 5L, 2L, 2L, 5L, 2L, 2L, 5L, 5L, 2L, 5L, 2L, 5L, 1L, 2L), .Label = c("short", "long", "between", "young", "old"), class = "factor")), .Names = c("org", "art", "type"), row.names = c(NA, -192L), class = "data.frame") and then the plot ggplot(exstatus, aes(x=type, fill=art))+ geom_bar(aes(y=..count../sum(..count..)),position="dodge") The problem is that the two rightmost bars ("young", "old") are too thick - "something" takes up the whole width - whcih is not what I intended. I am sorry that I can not explain it better.

    Read the article

  • how to use ggplot conditional on data

    - by Andreas
    I asked this question and it seams ggplot2 currently has a bug with empty data.frames. Therefore I am trying to check if the dataframe is empty, before I make the plot. But what ever I come up with, it gets really ugly, and doesn't work. So I am asking for your help. example data: SOdata <- structure(list(id = 10:55, one = c(7L, 8L, 7L, NA, 7L, 8L, 5L, 7L, 7L, 8L, NA, 10L, 8L, NA, NA, NA, NA, 6L, 5L, 6L, 8L, 4L, 7L, 6L, 9L, 7L, 5L, 6L, 7L, 6L, 5L, 8L, 8L, 7L, 7L, 6L, 6L, 8L, 6L, 8L, 8L, 7L, 7L, 5L, 5L, 8L), two = c(7L, NA, 8L, NA, 10L, 10L, 8L, 9L, 4L, 10L, NA, 10L, 9L, NA, NA, NA, NA, 7L, 8L, 9L, 10L, 9L, 8L, 8L, 8L, 8L, 8L, 9L, 10L, 8L, 8L, 8L, 10L, 9L, 10L, 8L, 9L, 10L, 8L, 8L, 7L, 10L, 8L, 9L, 7L, 9L), three = c(7L, 10L, 7L, NA, 10L, 10L, NA, 10L, NA, NA, NA, NA, 10L, NA, NA, 4L, NA, 7L, 7L, 4L, 10L, 10L, 7L, 4L, 7L, NA, 10L, 4L, 7L, 7L, 7L, 10L, 10L, 7L, 10L, 4L, 10L, 10L, 10L, 4L, 10L, 10L, 10L, 10L, 7L, 10L), four = c(7L, 10L, 4L, NA, 10L, 7L, NA, 7L, NA, NA, NA, NA, 10L, NA, NA, 4L, NA, 10L, 10L, 7L, 10L, 10L, 7L, 7L, 7L, NA, 10L, 7L, 4L, 10L, 4L, 7L, 10L, 2L, 10L, 4L, 12L, 4L, 7L, 10L, 10L, 12L, 12L, 4L, 7L, 10L), five = c(7L, NA, 6L, NA, 8L, 8L, 7L, NA, 9L, NA, NA, NA, 9L, NA, NA, NA, NA, 7L, 8L, NA, NA, 7L, 7L, 4L, NA, NA, NA, NA, 5L, 6L, 5L, 7L, 7L, 6L, 9L, NA, 10L, 7L, 8L, 5L, 7L, 10L, 7L, 4L, 5L, 10L), six = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("2010-05-25", "2010-05-27", "2010-06-07"), class = "factor"), seven = c(0.777777777777778, 0.833333333333333, 0.333333333333333, 0.888888888888889, 0.5, 0.888888888888889, 0.777777777777778, 0.722222222222222, 0.277777777777778, 0.611111111111111, 0.722222222222222, 1, 0.888888888888889, 0.722222222222222, 0.555555555555556, NA, 0, 0.666666666666667, 0.666666666666667, 0.833333333333333, 0.833333333333333, 0.833333333333333, 0.833333333333333, 0.722222222222222, 0.833333333333333, 0.888888888888889, 0.666666666666667, 1, 0.777777777777778, 0.722222222222222, 0.5, 0.833333333333333, 0.722222222222222, 0.388888888888889, 0.722222222222222, 1, 0.611111111111111, 0.777777777777778, 0.722222222222222, 0.944444444444444, 0.555555555555556, 0.666666666666667, 0.722222222222222, 0.444444444444444, 0.333333333333333, 0.777777777777778), eight = c(0.666666666666667, 0.333333333333333, 0.833333333333333, 0.666666666666667, 1, 1, 0.833333333333333, 0.166666666666667, 0.833333333333333, 0.833333333333333, 1, 1, 0.666666666666667, 0.666666666666667, 0.333333333333333, 0.5, 0, 0.666666666666667, 0.5, 1, 0.666666666666667, 0.5, 0.666666666666667, 0.666666666666667, 0.666666666666667, 0.333333333333333, 0.333333333333333, 1, 0.666666666666667, 0.833333333333333, 0.666666666666667, 0.666666666666667, 0.5, 0, 0.833333333333333, 1, 0.666666666666667, 0.5, 0.666666666666667, 0.666666666666667, 0.5, 1, 0.833333333333333, 0.666666666666667, 0.833333333333333, 0.666666666666667), nine = c(0.307692307692308, NA, 0.461538461538462, 0.538461538461538, 1, 0.769230769230769, 0.538461538461538, 0.692307692307692, 0, 0.153846153846154, 0.769230769230769, NA, 0.461538461538462, NA, NA, NA, NA, 0, 0.615384615384615, 0.615384615384615, 0.769230769230769, 0.384615384615385, 0.846153846153846, 0.923076923076923, 0.615384615384615, 0.692307692307692, 0.0769230769230769, 0.846153846153846, 0.384615384615385, 0.384615384615385, 0.461538461538462, 0.384615384615385, 0.461538461538462, NA, 0.923076923076923, 0.692307692307692, 0.615384615384615, 0.615384615384615, 0.769230769230769, 0.0769230769230769, 0.230769230769231, 0.692307692307692, 0.769230769230769, 0.230769230769231, 0.769230769230769, 0.615384615384615), ten = c(0.875, 0.625, 0.375, 0.75, 0.75, 0.75, 0.625, 0.875, 1, 0.125, 1, NA, 0.625, 0.75, 0.75, 0.375, NA, 0.625, 0.5, 0.75, 0.875, 0.625, 0.875, 0.75, 0.625, 0.875, 0.5, 0.75, 0, 0.5, 0.875, 1, 0.75, 0.125, 0.5, 0.5, 0.5, 0.625, 0.375, 0.625, 0.625, 0.75, 0.875, 0.375, 0, 0.875), elleven = c(1, 0.8, 0.7, 0.9, 0, 1, 0.9, 0.5, 0, 0.8, 0.8, NA, 0.8, NA, NA, 0.8, NA, 0.4, 0.8, 0.5, 1, 0.4, 0.5, 0.9, 0.8, 1, 0.8, 0.5, 0.3, 0.9, 0.2, 1, 0.8, 0.1, 1, 0.8, 0.5, 0.2, 0.7, 0.8, 1, 0.9, 0.6, 0.8, 0.2, 1), twelve = c(0.666666666666667, NA, 0.133333333333333, 1, 1, 0.8, 0.4, 0.733333333333333, NA, 0.933333333333333, NA, NA, 0.6, 0.533333333333333, NA, 0.533333333333333, NA, 0, 0.6, 0.533333333333333, 0.733333333333333, 0.6, 0.733333333333333, 0.666666666666667, 0.533333333333333, 0.733333333333333, 0.466666666666667, 0.733333333333333, 1, 0.733333333333333, 0.666666666666667, 0.533333333333333, NA, 0.533333333333333, 0.6, 0.866666666666667, 0.466666666666667, 0.533333333333333, 0.333333333333333, 0.6, 0.6, 0.866666666666667, 0.666666666666667, 0.6, 0.6, 0.533333333333333)), .Names = c("id", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "elleven", "twelve"), class = "data.frame", row.names = c(NA, -46L)) And the plot iqr <- function(x, ...) { qs <- quantile(as.numeric(x), c(0.25, 0.5, 0.75), na.rm = T) names(qs) <- c("ymin", "y", "ymax") qs } magic <- function(y, ...) { high <- median(SOdata[[y]], na.rm=T)+1.5*sd(SOdata[[y]],na.rm=T) low <- median(SOdata[[y]], na.rm=T)-1.5*sd(SOdata[[y]],na.rm=T) ggplot(SOdata, aes_string(x="six", y=y))+ stat_summary(fun.data="iqr", geom="crossbar", fill="grey", alpha=0.3)+ geom_point(data = SOdata[SOdata[[y]] > high,], position=position_jitter(w=0.1, h=0),col="green", alpha=0.5)+ geom_point(data = SOdata[SOdata[[y]] < low,], position=position_jitter(w=0.1, h=0),col="red", alpha=0.5)+ stat_summary(fun.y=median, geom="point",shape=18 ,size=4, col="orange") } for (i in names(SOdata)[-c(1,7)]) { p<- magic(i) ggsave(paste("magig_plot_",i,".png",sep=""), plot=p, height=3.5, width=5.5) } The problem is that sometimes in the call to geom_point the subset returns an empty dataframe, which sometimes (!) causes ggplot2 to plot all the data instead of none of the data. geom_point(data = SOdata[SOdata[[y]] > high,], position=position_jitter(w=0.1, h=0),col="green", alpha=0.5)+ This is kindda of important to me, and I am really stuck trying to find a solution. Any help that will get me started is much appreciated. Thanks in advance.

    Read the article

  • program R- in ggplot restrict y to be >0 in LOESS plot

    - by Nate
    Here's my code: qplot(data=sites, x, y, main="Site 349") (p <- qplot(data = sites, x, y, xlab = "", ylab = "")) (p1 <- p + geom_smooth(method = "loess",span=0.5, size = 1.5)) p1 + theme_bw() + opts(title = "Site 349") Some of the LOESS lines and confidence intervals go below zero, but I would like to restrict the graphics to 0 and positive numbers (because negative do not make sense). How can I do this?

    Read the article

  • Reading a user input (character or string of letters) into ggplot command inside a switch statement or a nested ifelse (with functions in it)

    - by statisticalbeginner
    I have code like AA <- as.integer(readline("Select any number")) switch(AA, 1={ num <-as.integer(readline("Select any one of the options \n")) print('You have selected option 1') #reading user data var <- readline("enter the variable name \n") #aggregating the data based on required condition gg1 <- aggregate(cbind(get(var))~Mi+hours,a, FUN=mean) #Ploting ggplot(gg1, aes(x = hours, y = get(var), group = Mi, fill = Mi, color = Mi)) + geom_point() + geom_smooth(stat="smooth", alpha = I(0.01)) }, 2={ print('bar') }, { print('default') } ) The dataset is [dataset][1] I have loaded the dataset into object list a <- read.table(file.choose(), header=FALSE,col.names= c("Ei","Mi","hours","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","par","Temp","Sal","co2atm","u10","dicfl","co2ppm","co2mol","pH")) I am getting error like source ("switch_statement_check.R") Select any one of the options 1 [1] "You have selected option 1" enter the variable name Nphy Error in eval(expr, envir, enclos) : (list) object cannot be coerced to type 'double' > gg1 is getting data that is fine. I dont know what to do to make the variable entered by user to work in that ggplot command. Please suggest any solution for this. The dput output structure(list(Ei = c(1L, 1L, 1L, 1L, 1L, 1L), Mi = c(1L, 1L, 1L, 1L, 1L, 1L), hours = 1:6, Nphy = c(0.1023488, 0.104524, 0.1064772, 0.1081702, 0.1095905, 0.110759), Cphy = c(0.6534707, 0.6448216, 0.6369597, 0.6299084, 0.6239005, 0.6191941), CHLphy = c(0.1053458, 0.110325, 0.1148174, 0.1187672, 0.122146, 0.1249877), Nhet = c(0.04994161, 0.04988347, 0.04982555, 0.04976784, 0.04971029, 0.04965285), Chet = c(0.3308593, 0.3304699, 0.3300819, 0.3296952, 0.3293089, 0.3289243), Ndet = c(0.04991916, 0.04984045, 0.04976363, 0.0496884, 0.04961446, 0.04954156), Cdet = c(0.3307085, 0.3301691, 0.3296314, 0.3290949, 0.3285598, 0.3280252), DON = c(0.05042275, 0.05085697, 0.05130091, 0.05175249, 0.05220978, 0.05267118 ), DOC = c(49.76304, 49.52745, 49.29323, 49.06034, 48.82878, 48.59851), DIN = c(14.9933, 14.98729, 14.98221, 14.9781, 14.97485, 14.97225), DIC = c(2050.132, 2050.264, 2050.396, 2050.524, 2050.641, 2050.758), AT = c(2150.007, 2150.007, 2150.007, 2150.007, 2150.007, 2150.007), dCCHO = c(0.964222, 0.930869, 0.8997098, 0.870544, 0.843196, 0.8175117), TEPC = c(0.1339044, 0.1652179, 0.1941872, 0.2210289, 0.2459341, 0.2690721), Ncocco = c(0.1040715, 0.1076058, 0.1104229, 0.1125141, 0.1140222, 0.1151228), Ccocco = c(0.6500288, 0.6386706, 0.6291149, 0.6213265, 0.6152447, 0.6108502), CHLcocco = c(0.1087667, 0.1164099, 0.1225822, 0.1273103, 0.1308843, 0.1336465), PICcocco = c(0.1000664, 0.1001396, 0.1007908, 0.101836, 0.1034179, 0.1055634), par = c(0, 0, 0.8695131, 1.551317, 2.777707, 4.814341), Temp = c(9.9, 9.9, 9.9, 9.9, 9.9, 9.9), Sal = c(31.31, 31.31, 31.31, 31.31, 31.31, 31.31), co2atm = c(370, 370, 370, 370, 370, 370), u10 = c(0.01, 0.01, 0.01, 0.01, 0.01, 0.01), dicfl = c(-2.963256, -2.971632, -2.980446, -2.989259, -2.997877, -3.005702), co2ppm = c(565.1855, 565.7373, 566.3179, 566.8983, 567.466, 567.9814), co2mol = c(0.02562326, 0.02564828, 0.0256746, 0.02570091, 0.02572665, 0.02575002 ), pH = c(7.879427, 7.879042, 7.878636, 7.878231, 7.877835, 7.877475)), .Names = c("Ei", "Mi", "hours", "Nphy", "Cphy", "CHLphy", "Nhet", "Chet", "Ndet", "Cdet", "DON", "DOC", "DIN", "DIC", "AT", "dCCHO", "TEPC", "Ncocco", "Ccocco", "CHLcocco", "PICcocco", "par", "Temp", "Sal", "co2atm", "u10", "dicfl", "co2ppm", "co2mol", "pH"), row.names = c(NA, 6L), class = "data.frame") As per the below suggestions I have tried a lot but it is not working. Summarizing I will say: var <- readline("enter a variable name") I cant use get(var) inside any command but not inside ggplot, it wont work. gg1$var it also doesnt work, even after changing the column names. Does it have a solution or should I just choose to import from an excel sheet, thats better? Tried with if else and functions fun1 <- function() { print('You have selected option 1') my <- as.character((readline("enter the variable name \n"))) gg1 <- aggregate(cbind(get(my))~Mi+hours,a, FUN=mean) names(gg1)[3] <- my #print(names(gg1)) ggplot (gg1,aes_string(x="hours",y=(my),group="Mi",color="Mi")) + geom_point() } my <- as.integer(readline("enter a number")) ifelse(my == 1,fun1(),"") ifelse(my == 2,print ("its 2"),"") ifelse(my == 3,print ("its 3"),"") ifelse(my != (1 || 2|| 3) ,print("wrong number"),"") Not working either...:(

    Read the article

  • Can one extract model fit parameters after a ggplot stat_smooth call?

    - by Alex Holcombe
    Using stat_smooth, I can fit models to data. E.g. g=ggplot(tips,aes(x=tip,y=as.numeric(unclass(factor(tips$sex))-1))) +facet_grid(time~.) g=g+ stat_summary(fun.y=mean,geom="point") g=g+ stat_smooth(method="glm", family="binomial") I would like to know the coefficients of the glm binomial fits. I could re-do the fit with dlply and get the coefficients with ldply, but I'd like to avoid such duplication. Calling str(g) reveals the hierarchy of objects that ggplot creates, perhaps there's some way to get to the coefficients through that?

    Read the article

  • R ggplot barplot; Fill based on two separate variables

    - by user1476968
    A picture says more than a thousand words. As you can see, my fill is based on the variable variable. Within each bar there is however multiple data entities (black borders) since the discrete variable complexity make them unique. What I am trying to find is something that makes each section of the bar more distinguishable than the current look. Preferable would be if it was something like shading. Here's an example (not the same dataset, since the original was imported): dat <- read.table(text = "Complexity Method Sens Spec MMC 1 L Alpha 50 20 10 2 M Alpha 40 30 80 3 H Alpha 10 10 5 4 L Beta 70 50 60 5 M Beta 49 10 80 6 H Beta 90 17 48 7 L Gamma 19 5 93 8 M Gamma 18 39 4 9 H Gamma 10 84 74", sep = "", header=T) library(ggplot2) library(reshape) short.m <- melt(dat) ggplot(short.m, aes(x=Method, y= value/100 , fill=variable)) + geom_bar(stat="identity",position="dodge", colour="black") + coord_flip()

    Read the article

  • How to use an excel data-set for a multi-line ggplot in R?

    - by user1299887
    I have a data set in excel that I am trying to create a multiple line plot with on R. The data set contains 7 food groups and the calories consumed daily associated to the groups. As well, there is that set of data over 38 years (from 1970-2008) and I am attempting to use this data set to create a multiple line plot on R. I have tried for hours on end but can not seem to get R to recognize the variables within the data set.

    Read the article

  • object of type 'closure' is not subsettable - contradiction?

    - by Alex
    I'm writing a function to produce time series plots of stock prices. However, I'm getting the following error "Error in df[, 7] : object of type 'closure' is not subsettable" Here's an example of the function: plot.prices <- function(df) { require(ggplot2) g <- ggplot(df, aes(x= as.Date(Date, format= "%Y-%m-%d"), y= df[, 7])) + geom_point(size=1) # ... code not shown... g } And example data: spy <- read.csv(file= 'http://ichart.finance.yahoo.com/table.csv?s=SPY&d=11&e=1&f=2012&g=d&a=0&b=29&c=1993&ignore=.csv', header= T) plot.prices(spy) # produces error ggplot(spy, aes(x= as.Date(Date, format= "%Y-%m-%d"), y= spy[, 7])) + geom_point(size=1) ## does not produce error As you can see, the code is identical. I get an error if the call to ggplot() is INSIDE the function but not if the call to ggplot() is OUTSIDE the function. Anyone have any idea why the seeming contradiction?

    Read the article

1 2 3  | Next Page >