R: Issues with line graph of germination trough time - r
I'm still in the process of learning R using Swirl and RStudio, and a goal I've set for myself is to recreate this graph. I have a small dataset that I will link below (it's saved as a plain text CSV file that I import into R with headings enabled).
If I try to plot that dataset without changing anything, I get this, which is obviously not the goal.
At first I thought the problem would be in the class of my imported dataset, defined as kt. After class(kt) turned out to be data.frame I figured that wasn't the problem. Should I be trying to rewrite the table to something that R can plot instantly, or should I be trying to extract each species individually, plot them separately and then combining the different plots into one graph? Perhaps there is something wrong with my dates, I know that R handles dates in a specific way. Maybe these solutions are not even needed and I'm just doing something stupidly simple wrong, but I can't find it myself.
Your help is much appreciated.
Dataset:
Species,week 0,week 1,week 2,week 3,week 4,week 5,week 6,week 7,week 8,week 9,week 10,week 11,week 12,week 13,week 14,week 15,week 16,week 17,week 18
Caesalpinia coriaria,0.0%,24.0%,28.0%,28.0%,32.0%,37.0%,40.0%,46.0%,52.0%,56.0%,63.0%,64.0%,68.0%,71.0%,72.0%,,,,
Coccoloba swartzii,0.0%,0.0%,1.0%,10.0%,19.0%,31.0%,33.0%,39.0%,43.0%,48.0%,52.0%,52.0%,52.0%,52.0%,52.0%,52.0%,52.0%,55.0%,
Cordia dentata,0.0%,5.0%,18.0%,21.0%,24.0%,26.0%,27.0%,30.0%,32.0%,32.0%,32.0%,32.0%,32.0%,32.0%,33.0%,33.0%,33.0%,34.0%,35.0%
Guaiacum officinale,0.0%,0.0%,0.0%,0.0%,4.0%,5.0%,5.0%,5.0%,7.0%,8.0%,8.0%,8.0%,8.0%,8.0%,8.0%,8.0%,8.0%,,
Randia aculeata,0.0%,0.0%,0.0%,4.0%,13.0%,14.0%,18.0%,19.0%,21.0%,21.0%,21.0%,21.0%,21.0%,22.0%,22.0%,22.0%,22.0%,,
Schoepfia schreberi,0.0%,0.0%,0.0%,0.0%,0.0%,0.0%,1.0%,4.0%,8.0%,11.0%,13.0%,21.0%,21.0%,24.0%,24.0%,25.0%,27.0%,,
Prosopis juliflora,0.0%,7.5%,31.3%,34.2%,,,,,,,,,,,,,,,
Something like this??
# get rid of "%" signs
df <- data.frame(sapply(df,function(x)gsub("%","",x,fixed=T)))
# convert cols 2:20 to numeric
df[,2:20] <- sapply(df[,2:20],function(x)as.numeric(as.character(x)))
library(reshape2)
library(ggplot2)
gg <- melt(df,id="Species")
ggplot(gg,aes(x=variable,y=value,color=Species,group=Species)) +
geom_line()+
theme_bw()+
theme(legend.position="bottom", legend.title=element_blank())
There are lots of problems here.
First, if your dataset really has those % signs, then R interprets the data as character and imports it as factors. So first we have to get rid of the % (using gsub(...), and then we have to convert what's left to numeric. With factors, you have to convert to character first, then numeric, so: as.numeric(as.character(...)). All of this could have been avoided if you exported the data without the % signs!!!
Plotting multiple curves with different colors is something the ggplot package was designed for (among many other things), so we use that. ggplot prefers data in "long" format - all the data in one column, with a second column distinguishing different datasets. Your data is in "wide" format - data in different columns. So we convert to long using melt(...) from the reshape2 package. The result, gg has three columns: Species, variable and value. value contains the actual data and variable contains the week number.
So now we create a ggplot object, setting the x-axis to the variable column, the y-axis to the value column, with color mapped to Species, and we tell ggplot to plot lines (using geom_line(...)).
The rest is to position the legend at the bottom, and turn off some of the ggplot default formatting.
Related
How to use corrplot with is.corr=FALSE
I previously made a beautiful functional and perfect actual corrolation plot with corrplot (my plot). Now I have to get the underlying data in the same look. So my goal is to have triangular similarity matrixes in the same colours as my corrolation plot. Imagine it like the conditional formatting in excel. My Data: my Data from excel Link to CSV Data file it is loaded in as a csv and it can read the csv perfectly My Code:corrplot(Phylogeny, is.corr=FALSE,method="number", cl.lim=c(0,1)) The error it throws me: Error in if (any(corr < cl.lim[1]) || any(corr > cl.lim[2])) { : Missing value, where TRUE/FALSE is required i made sure all colums are numeric i made sure to fill the missing bits with NA's (because that was a problem somwhere before) i made sure all my values are between 0 and 1 like i want the limit to be (in between it told me that my values are not within the limit, when i tried around with some stuff) the error does not change when i change the limit the error does not change when i take the is.corr=FALSE out (default=TRUE) i played around with corrplot.mixed and its still not working have been referencing information from Corrplot Intro I have looked into the condformat function but i am not really sure if it can do a filling of each cell with one colour according to the overall gradient like i used for my corrolation plot. What am I missing here that it does not want to give me my table back with pretty colours?
I had the same error, but I was able to fix it by converting my data.frame to a matrix. I ended up with corrplot(as.matrix(df), is.corr = FALSE).
If I am understanding correctly, your posted data are already a correlation matrix - although not a fully symmetrical one of the sort that would be produced with the call cor on raw data. In that case, the problem is just that you have variable names (Species) as a column in your data. Change this column to row names, drop the variable names, and call corrplot as user9536160 suggests: # read in your data phyl <- as.data.frame(read_csv("Phylogeny.csv")) # name rows and drop variable names in the df itself row.names(phyl) <- phyl$Species phyl <- phyl %>% select(-Species) # call corrplot corrplot(as.matrix(phyl), is.corr = FALSE) The result:
Choropleth Plotting polygons with ggplot2 R on a map
I realise this has been asked about 100 times prior, but none of the answers I've read so far on SO seem to fit my problem. I have data. I have the lat and lon values. I've read around about something called sp and made a bunch of shape objects in a dataframe. I have matched this dataframe with the variable I am interested in mapping. I cannot for the life of me figure out how the hell to get ggplot2 to draw polygons. Sometimes it wants explicit x,y values (which are a PART of the shape anyway, so seems redundant), or some other shape files externally which I don't actually have. Short of colouring it in with highlighters, I'm at a loss. if I take an individual sps object (built with the following function after importing, cleaning, and wrangling a shitload of data) createShape = function(sub){ #This funciton takes the list of lat/lng values and returns a SHAPE which should be plottable on ggmap/ggplot tempData = as.data.frame(do.call(rbind, as.list(VICshapes[which(VICshapes$Suburb==sub),] %>% select(coords))[[1]][[1]])) names(tempData) = c('lat', 'lng') p = Polygon(tempData) ps = Polygons(list(p),1) sps = SpatialPolygons(list(ps)) return(sps) } These shapes are then stored in the same dataframe as my data - which only this afternoon for some reason, I can't even look at, as trying to look at it yields the following error. head(plotdata) Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L, : first argument must be atomic I realise I'm really annoyed at this now, but I've about 70% of a grade riding on this, and my university has nobody capable of assisting. I have pasted the first few rows of data here - https://pastebin.com/vFqy5m5U - apparently you can't print data with an s4 object - the shape file that I"m trying to plot. Anyway. I'm trying to plot each of those shapes onto a map. Polygons want an x,y value. I don't have ANY OTHER SHAPE FILES. I created them based on a giant list of lat and long values, and the code chunk above. I'm genuinely at a loss here and don't know what question to even ask. I have the variable of interest based on locality, and the shape for each locality. What am I missing? edit: I've pasted the summary data (BEFORE making them into shapes) here. It's a massive list of lat/lng values for EACH tile/area, so it's pretty big...
Answered on gis.stackexchange.com (link not provided).
Getting subscripts from Excel into R
I just startet learning R but I already have my first problem. I want to disply my data in a graph. My data is in an Excel sheet converted to a .csv sheet. But I have some chemical formulars like Fe2O3 in my data and with the .csv all subscripst are gone. That doesn't look very nice. Is there any way to get the subscripts from the original Excel file into R? I would really appreciate your help :) Edit: My data contains 6 chemical formulars displayed on the x-axis, which all contain subscripts (i.e. Fe2O3, ZnCl2, CO2, ...) and nummeric values displayed on the y-axis. The graph is a bar chart. I am not sure if there is a way to either change the numbers to subscipts in R or keep them prior to the import. The graph looks like this. But I would like to have the numbers as subscripts:
I don't know that there's a way to bring the formatting from excel into a CSV and then R, unless you can make those subscripts using unicode. UTF8 symbols for subscript letters Given that your list of chemicals is short, it's not much work to tweak the chemical names to help ggplot interpret them with subscripts. You'll want brackets around the numbers, plus tildes afterwards if there are more elements to include. Then we also tell scale_x_discrete to "parse" the labels and convert those symbols to formatting. set.seed(42) chem_df <- tibble( Chemicals = c("AgNO3", "Al2SiO5", "CO2", "Fe2O3", "FeSO4", "ZnCl2"), Chemicals_parsed = c("AgNO[3]", "Al[2]~SiO[5]", "CO[2]", "Fe[2]~O[3]", "FeSO[4]", "ZnCl[2]"), Mean = rnorm(6, 50, 30)) ggplot(chem_df, aes(x=Chemicals_parsed, Mean)) + geom_col() + scale_x_discrete(name = "Chemicals", labels=parse(text=chem_df$Chemicals_parsed))
To add to the excellent answer of #JonSpring, you can write a function which will convert strings like ""Al2SiO5" to strings like "Al[2]~SiO[5]", so you don't have to manually make all the conversions: library(stringr) chem.form <- function(s){ s <- str_replace_all(s,"([0-9]+)","[\\1]~") if(endsWith(s,"~")) s <- substr(s,1,nchar(s) - 1) s } Chemicals <- c("AgNO3", "Al2SiO5", "CO2", "Fe2O3", "FeSO4", "ZnCl2") Chemicals_parsed <- as.vector(sapply(Chemicals,chem.form))
csv to frequency polygon using R or python
I have a result.csv file to which contains information in the following format : date,tweets 2015-06-15,tweet 2015-06-15,tweet 2015-06-12,tweet 2015-06-11,tweet 2015-06-11,tweet 2015-06-11,tweet 2015-06-08,tweet 2015-06-08,tweet i want to plot a frequency polygon with number of entries corresponding to each date as y axis and dates as x axis i have tried the following code : pf<-read.csv("result.csv") library(ggplot2) qplot(datetime, data =pf, geom = "freqpoly") but it shows the following error : geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic? can anyone tell me how to solve this problem. I am totally new to R so any kind of guidance will be of great help to me
Your issue is that you are trying to treat datetime as continuous, but it's imported it as a factor (discrete/categorical). Let's convert it to a Date object and then things should work: pf$datetime = as.Date(pf$datetime) qplot(datetime, data =pf, geom = "freqpoly")
Based on your code, I assume that the result.csv has a header: datetime, atweet. By default, read.csv takes the first line of the CSV file as column names. That means you will be able to access the two columns with pf$datetime and pf$atweet. If you look at the documentation of read.csv, you will find that stringsAsFactors = default.stringsAsFactors(), which is FALSE. That is, the strings from CSV files are kept as factors. Now, even if you change the value of stringsAsFactors, you still get the same error. That is because ggplot does not know how to order the dates, as it does not recognize the strings as such. To transform the strings into logical dates, you can use strptime. Here is the working example: pf<-read.csv("result.csv", stringsAsFactors=FALSE) library(ggplot2) qplot(strptime(pf$datetime, "%Y-%m-%d"), data=pf, geom='freqpoly')
How to plot several line plots in one
I would like to plot my figure using R (ggplot2). I'd like to have a line graph like image 2. here my.data: B50K,B50K+1000C50K,B50K+2000C50K,B50K+4000C50K,B50K+8000C50K,gen,xaxile 0.3795,0.4192,0.4675,0.5357,0.6217,T18-Yield,B50K 0.3178,0.3758,0.4249,0.5010,0.5870,T20-Yield,B50K+1000C50K 0.2795,0.3266,0.3763,0.4636,0.5583,T21-Yield,B50K+2000C50K 0.2417,0.2599,0.2898,0.3291,0.3736,T18-Fertility,B50K+4000C50K 0.2002,0.2287,0.2531,0.2962,0.3485,T19-Fertility,B50K+8000C50K 0.1642,0.1911,0.2151,0.2544,0.2951,T20-Fertility ***--> The delimiter is ",". By the way, I have not any useful .r script which would be helpful or useful. The illustrated image shows my figure in Microsoft word. I have tried several scripts via internet but non of them have not worked. would you please help me to have a .r script to read my data file like img1 and plot my data like illustrated figure.
The trick is to reshape your data (using melt from the reshape2 package) so that you can easily map colours and linetypes to gen. # Your data - note i also added an extra comma after the fifth column in row 6. # It would be easier if you gave data using dput as described in comments above - thanks dat <- read.table(text="B50K,B50K+1000C50K,B50K+2000C50K,B50K+4000C50K,B50K+8000C50K,xaxile,gen 0.3795,0.4192,0.4675,0.5357,0.6217,B50K,T18-Yield 0.3178,0.3758,0.4249,0.5010,0.5870,B50K+1000C50K,T20-Yield 0.2795,0.3266,0.3763,0.4636,0.5583,B50K+2000C50K,T21-Yield 0.2417,0.2599,0.2898,0.3291,0.3736,B50K+4000C50K,T18-Fertility 0.2002,0.2287,0.2531,0.2962,0.3485,B50K+8000C50K,T19-Fertility 0.1642,0.1911,0.2151,0.2544,0.2951,,T20-Fertility", header=T, sep=",", na.strings="") # load the pckages you need library(ggplot2) library(reshape2) # assume xaxile column is unneeded? - did you add this column yourself? dat$xaxile <- NULL # reshape data for plotting dat.m <- melt(dat) # plot ggplot(dat.m, aes(x=variable, y=value, colour=gen, shape=gen, linetype=gen, group=gen)) + geom_point() + geom_line() You can then use scale_linetype_manual and scale_shape_manual to manually specify how you want the plot to look. This post will help, but there are many others as well