I have the following code for a calendar heatmap. I have created one previously using this code. But when I try to create another one, I enter the code and press enter but plus signs appear and I cannot execute.
library(ggplot2)
source("https://raw.githubusercontent.com/iascchen/VisHealth/master/R/calendarHeat.R")
library(plyr)
library(plotly)
r2g <- c("#D61818", "#B5E384")
calendarHeat(heatmap1$date, heatmap1$ROI, ncolors = 2, color = "r2g", varname="30-day ROI") # here you had backquotes at the end of the line
heatmap1 is the name of the data.
A cut of the data is shown below
Row Date ROI
1 2010-08-17 0
2 2010-08-18 0
3 2010-08-19 0
4 2010-08-20 0
5 2010-08-21 1
6 2010-08-22 1
7 2010-08-23 1
8 2010-08-24 1
9 2010-08-25 1
10 2010-08-26 1
11 2010-08-27 1
12 2010-08-28 0
13 2010-08-29 0
14 2010-08-30 0
15 2010-08-31 0
16 2010-09-01 1
17 2010-09-02 1
18 2010-09-03 1
19 2010-09-04 1
20 2010-09-05 0
21 2010-09-06 1
22 2010-09-07 1
23 2010-09-08 0
24 2010-09-09 0
25 2010-09-10 0
26 2010-09-11 0
27 2010-09-12 0
28 2010-09-13 0
29 2010-09-14 0
30 2010-09-15 0
31 2010-09-16 0
I don't understand why the code will work when executed previously, but now doesn't work. Any ideas?
There was a syntax error with quotes. R will propose you to continue typing a line with + if there is no match to ',",{ and(.
This is explained in this pdf, page 4-5.
Related
I have 100 yrs precipitation and temperature data that I would like to analyze using the 'seas' package. I have tried several formats for the date column and get the following error code every time:
Error in seas.df.check(x, orig, var) :
a ‘date’ column must exist in ‘srs_precip’
Example code below, where I have tried to coerce Gregorian dates into Date, also Date.POSIX but still get error message from 'seas' library.
Year yr.m.d Date date.J Inches mm_SRS Max_Temp_F Max_Temp_C Min_Temp_F Min_Temp_C Date.POSIX
1 1919 1919-01-01 1919-01-01 1 0 0 39 3.888889 26 -3.333333333 1919-01-01
2 1919 1919-01-02 1919-01-02 2 0 0 35 1.666667 19 -7.222222222 1919-01-02
3 1919 1919-01-03 1919-01-03 3 0 0 40 4.444444 14 -10 1919-01-03
4 1919 1919-01-04 1919-01-04 4 0 0 52 11.111111 20 -6.666666667 1919-01-04
5 1919 1919-01-05 1919-01-05 5 0 0 43 6.111111 20 -6.666666667 1919-01-05
6 1919 1919-01-06 1919-01-06 6 0 0 56 13.333333 31 -0.555555556 1919-01-06
I created this table:
> head(table)
tissue1 tissue2 tissue3 tissue4 tissue5
Simple_repeat_80 58 77 48 69 115 131
tRNA_1 0 14 12 1 19 14
Simple_repeat_86 2 10 2 2 14 9
Simple_repeat_87 1 33 12 3 15 21
Simple_repeat_103 0 0 2 0 0 4
SINE/tRNA-Deu_20 0 0 1 0 0 10
and I put the command
row <- strsplit(rownames(table), "_[0-9]+") to eliminate the underscore and the number of the elements after the name. I would to create a new table like this example:
> head(table)
tissue1 tissue2 tissue3 tissue4 tissue5
Simple_repeat 58 77 48 69 115 131
tRNA 0 14 12 1 19 14
Simple_repeat 2 10 2 2 14 9
Simple_repeat 1 33 12 3 15 21
Simple_repeat 0 0 2 0 0 4
SINE/tRNA-Deu 0 0 1 0 0 10
I've tried this command:
> row.names(table) = row
Error in `.rowNamesDF<-`(x, value = value) :
'row.names' duplicate non sono permesse
Inoltre: Warning message:
non-unique values when setting 'row.names': ‘DNA?’, ‘DNA/hAT-Ac’, ‘DNA/hAT-Charlie’, ‘DNA/hAT-Tag1’, ‘DNA/hAT-Tip100’, ‘DNA/MULE-MuDR’, ‘DNA/PIF-Harbinger’, ‘DNA/PiggyBac’, ‘DNA/TcMar-Mariner’, ‘DNA/TcMar-Tc1’, ‘DNA/TcMar-Tc2’, ‘DNA/TcMar-Tigger’, ‘LINE/CR1’, ‘LINE/Dong-R4’, ‘LINE/I-Jockey’, ‘LINE/L1’, ‘LINE/L2’, ‘LINE/Penelope’, ‘LINE/RTE-BovB’, ‘Low_complexity’, ‘LTR/ERV1’, ‘LTR/ERVK’, ‘LTR/ERVL’, ‘LTR/Gypsy’, ‘LTR/Gypsy?’, ‘RC/Helitron’, ‘rRNA’, ‘Satellite/acro’, ‘Simple_repeat’, ‘SINE/5S-Deu-L2’, ‘SINE/MIR’, ‘SINE/tRNA’, ‘SINE/tRNA-Deu’, ‘SINE/tRNA-RTE’, ‘snRNA’, ‘srpRNA’, ‘tRNA’
How can I solve it?
Your issue is that you are trying to assign duplicate row.names, which is not legal - multiple rows would be named Simple_repeat. One solution is to make the names unique, for example with:
row.names(table) <- make.unique(row)
Another solution is to not make use of row names at all, but create a separate column and then use that for further processing instead of row names, e.g.
table$rowLabel <- row
I have a table like:
From/To (%) 0 30 60 90 NA
1 0 63 24 0 0 13
2 30 28 46 21 0 5
3 60 13 29 25 2 31
4 90 7 2 5 52 35
5 NA 93 7 1 0 0
I'd like to save it as a .png
png("C:/exp/test.png", height=1000, width=200)
pandoc.table(a, justify='right', style='simple',emphasize.rownames=T,
digits= '2')
dev.off()
But I got an empty file.
How can I save it as an image and also with no extra margin (no blank spaces)?
Consider a data frame df with an extract from a web server access log, with two fields (sample below, duration is in msec and to simplify the example, let's ignore the date).
time,duration
18:17:26.552,8
18:17:26.632,10
18:17:26.681,12
18:17:26.733,4
18:17:26.778,5
18:17:26.832,5
18:17:26.889,4
18:17:26.931,3
18:17:26.991,3
18:17:27.040,5
18:17:27.157,4
18:17:27.209,14
18:17:27.249,4
18:17:27.303,4
18:17:27.356,13
18:17:27.408,13
18:17:27.450,3
18:17:27.506,13
18:17:27.546,3
18:17:27.616,4
18:17:27.664,4
18:17:27.718,3
18:17:27.796,10
18:17:27.856,3
18:17:27.909,3
18:17:27.974,3
18:17:28.029,3
qplot(time, duration, data=df); gives me a graph of the duration. I'd like to add, superimposed a line showing the number of requests for each minute. Ideally, this line would have a single data point per minute, at the :30sec point. If that's too complicated, an acceptable alternative is to have a step line, with the same value (the count of request) during a minute.
One way is to trunc(df$time, units=c("mins")), then calculate the count of request per minute into a new column then graph it.
I'm asking if there is, perhaps, a more direct way to accomplish the above. Thanks.
Following may be helpful. Create a data frame with steps and plot:
time duration sec sec2 diffsec2 step30s steps
1 18:17:26.552 8 26.552 552 0 0 0
2 18:17:26.632 10 26.632 632 80 1 1
3 18:17:26.681 12 26.681 681 49 0 0
4 18:17:26.733 4 26.733 733 52 1 1
5 18:17:26.778 5 26.778 778 45 0 0
6 18:17:26.832 5 26.832 832 54 1 1
7 18:17:26.889 4 26.889 889 57 1 2
8 18:17:26.931 3 26.931 931 42 0 0
9 18:17:26.991 3 26.991 991 60 1 1
10 18:17:27.040 5 27.040 040 -951 0 0
11 18:17:27.157 4 27.157 157 117 1 1
12 18:17:27.209 14 27.209 209 52 1 2
13 18:17:27.249 4 27.249 249 40 0 0
14 18:17:27.303 4 27.303 303 54 1 1
15 18:17:27.356 13 27.356 356 53 1 2
16 18:17:27.408 13 27.408 408 52 1 3
17 18:17:27.450 3 27.450 450 42 0 0
18 18:17:27.506 13 27.506 506 56 1 1
19 18:17:27.546 3 27.546 546 40 0 0
20 18:17:27.616 4 27.616 616 70 1 1
21 18:17:27.664 4 27.664 664 48 0 0
22 18:17:27.718 3 27.718 718 54 1 1
23 18:17:27.796 10 27.796 796 78 1 2
24 18:17:27.856 3 27.856 856 60 1 3
25 18:17:27.909 3 27.909 909 53 1 4
26 18:17:27.974 3 27.974 974 65 1 5
27 18:17:28.029 3 28.029 029 -945 0 0
>
> ggplot(ddf)+geom_point(aes(x=time, y=duration))+geom_line(aes(x=time, y=steps, group=1),color='red')
I have different dataframes with a column in which there are the latitudes (latitude) of some records and in another column of the same dataframe the date of the records (datecollected).
I would like to count and export in a new dataframe the number of the records in the same intervals of latitude (5 degrees) and year (two years).
(Hint: you'll make it easier for us to answer by providing some sample data.)
dataset <- data.frame(datecollected=
sample(as.Date("2000-01-01")+(0:3650),1000,replace=TRUE),
latitude=90*runif(1000))
We round the datecollected down to the next even year:
year.index <- (as.POSIXlt(dataset$datecollected)$year %/% 2)*2+1900
Similarly, we round the latitude down to the nearest multiple of 5 degrees:
latitude.index <- (floor(dataset$latitude) %/% 5)*5
Then we simply build a table on the rounded years and latitudes:
table(year.index,latitude.index)
latitude.index
year.index 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85
2000 12 9 15 7 11 10 11 14 9 13 11 10 8 11 13 25 10 18
2002 11 9 11 16 11 15 12 5 12 13 7 15 8 7 11 7 10 13
2004 8 12 9 10 12 16 12 13 9 7 16 11 6 13 4 15 12 10
2006 14 8 13 10 12 9 12 9 6 11 11 9 13 9 10 5 5 12
2008 8 12 17 12 12 8 12 8 14 12 11 11 10 10 14 16 17 13
EDIT: after a bit of discussion in the comments, I'll post my current script. It seems like there may be an issue when you read the data into R. This is what I do and what I get:
rm(list=ls())
dataset <- read.csv("GADUS.csv",header=TRUE,sep=",")
year.index <- (as.POSIXlt(as.character(dataset$datecollected),format="%Y-%m-%d")$year
%/% 2)*2+1900
latitude.index <- (floor(dataset$latitude) %/% 5)*5
table(year.index,latitude.index)
latitude.index
year.index 0 5 20 35 40 45 50 55 60 65 70 75
1752 0 0 0 0 0 20 0 0 0 0 0 0
1754 0 0 0 0 0 27 0 3 0 0 0 0
1756 0 0 0 0 0 21 0 1 0 0 0 0
1758 0 0 0 0 0 46 0 2 0 0 0 0
...
Does this give the same result for you? If not, please edit your question and post the result of str(dataset[,c("datecollected","latitude")]).