Here is my code to generate barplot using rAmChart,
library(rAmCharts)
amBarplot(x = "month", y = "value", data = dataset,
dataDateFormat = "MM/YYYY", minPeriod = "MM",
show_values = FALSE, labelRotation = -90, depth = 0.1)
However, is there a way to use month names & year in my x axis? I am trying to use MMM-YY formats.
Sample dataset,
structure(list(value = c(11544, 9588, 9411, 10365, 11154, 12688
), month = c("05/2012", "06/2012", "07/2012", "08/2012", "09/2012",
"10/2012")), .Names = c("value", "month"), row.names = c(NA,
6L), class = "data.frame")
Thanks.
It appears that rAmCharts doesn't expose AmCharts' dateFormats setting in the categoryAxis, so you have to access it through the init event and create your own dateFormats array with a modified format string for the MM period. I'm not very experienced with R, but here's how I managed to make it work using R 3.4.2 and rAmCharts 2.1.5
chart <- amBarplot( ... settings omitted ... )
addListener(.Object = chart,
name = 'init',
expression = paste(
"function(e) {",
"e.chart.categoryAxis.dateFormats = ",
'[{"period":"fff","format":"JJ:NN:SS"},{"period":"ss","format":"JJ:NN:SS"},',
'{"period":"mm","format":"JJ:NN"},{"period":"hh","format":"JJ:NN"},{"period":"DD","format":"MMM DD"},',
'{"period":"WW","format":"MMM DD"},',
'{"period":"MM","format":"MMM-YY"},', # "add YY to default MM format
'{"period":"YYYY","format":"YYYY"}]; ',
'e.chart.validateData();',
"}")
)
Here is a different solution:
library(rAmCharts)
dataset <- structure(list(value = c(11544, 9588, 9411, 10365, 11154, 12688
), month = c("05/2012", "06/2012", "07/2012", "08/2012", "09/2012",
"10/2012")), .Names = c("value", "month"), row.names = c(NA,
6L), class = "data.frame")
dataset$month <- as.character(
format(
as.Date(paste0("01/",dataset$month), "%d/%m/%Y"),
"%B %Y"))
amBarplot(x = "month", y = "value", data = dataset,
show_values = FALSE, labelRotation = -90, depth = 0.1)
Related
Looking to create a gt/reactable table in R that serves as a calendar heatmap. Something just like the one found on this site https://glin.github.io/reactable/articles/cookbook/cookbook.html. When I try to replicate that code, I get an error: "only defined on a data frame with all numeric-alike variables." I made Year a factor variable and do not want to color that column. Here is the code I tried + the dput output:
BuYlRd <- function(x) rgb(colorRamp(c("#7fb7d7", "#ffffbf", "#fc8d59"))
(x), maxColorValue = 255)
reactable(
bls,
defaultColDef = colDef(
style = function(value) {
if (!is.numeric(value)) return()
normalized <- (value - min(bls)) / (max(bls) - min(bls))
color <- BuYlRd(normalized)
list(background = color)
},
format = colFormat(digits = 2),
minWidth = 50
),
columns = list(
.rownames = colDef(name = "Year", sortable = TRUE, align =
"left")
),
bordered = TRUE
)
dput(head(bls))
structure(list(year = structure(1:3, .Label = c("2018", "2019",
"2020"), class = "factor"), January = c(329.5, 329.6, 327.5),
February = c(354.4, 328.5, 323.7), March = c(354.4, 324,
324.9), April = c(348.7, 326.9, 319.8), May = c(340.2, 321,
320.7), June = c(338, 316.1, 320.4), July = c(342.3, 317.3,
319), August = c(346.8, 317.3, 317.2), September = c(344.9,
317.3, 317), October = c(342.4, 318.2, 317.3), November =
c(334.4,
317.3, 328.2), December = c(335.5, 317.4, 328.7)), row.names =
c(NA,
-3L), class = c("tbl_df", "tbl", "data.frame"))
A solution could be to put the year column into the rownames of your dataset, and removing the year column thereafter :
bls=as.data.frame(bls)
rownames(bls)=bls$year
bls=bls[,-1]
reactable(
bls,
defaultColDef = colDef(
style = function(value) {
if (!is.numeric(value)) return()
normalized <- (value - min(bls)) / (max(bls) - min(bls))
color <- BuYlRd(normalized)
list(background = color)
},
format = colFormat(digits = 2),
minWidth = 50
),
columns = list(
.rownames = colDef(name = "Year", sortable = TRUE, align =
"left")
),
bordered = TRUE
)
I have this dataset:
structure(list(AgeGroup = c("Old", "Young"), Point.1 = c(0.401899407258065,
0.432845035714286), Point.2 = c(0.435610404, 0.448826385964912
), Point.3 = c(0.466951088, 0.473339649122807), Point.4 = c(0.490997664,
0.505416649122807), Point.5 = c(0.51047508, 0.517228789473684
), Point.6 = c(0.519614064, 0.520077087719298), Point.7 = c(0.524924236,
0.522939438596491), Point.8 = c(0.535448152, 0.523846929824561
), Point.9 = c(0.539298204, 0.529132140350877), Point.10 = c(0.546288992,
0.535221877192982), Point.11 = c(0.552286756, 0.544069684210526
), Point.12 = c(0.548644056, 0.547988701754386), Point.13 = c(0.546028996,
0.556100789473684), Point.14 = c(0.551464336, 0.557342807017544
), Point.15 = c(0.55835804, 0.552995140350877), Point.16 = c(0.560958056,
0.555753035087719), Point.17 = c(0.566631508, 0.553254912280702
), Point.18 = c(0.573644824, 0.557015719298246), Point.19 = c(0.579504268,
0.560797315789474), Point.20 = c(0.583600364, 0.560459526315789
), Point.21 = c(0.591889884, 0.563138245614035), Point.22 = c(0.598549332,
0.578847140350877), Point.23 = c(0.605920632, 0.59655149122807
), Point.24 = c(0.612308084, 0.611475473684211), Point.25 = c(0.618838952,
0.627695631578947), Point.26 = c(0.626865524, 0.640329719298246
), Point.27 = c(0.634642932, 0.642362438596491), Point.28 = c(0.639958892,
0.640706877192982), Point.29 = c(0.642219468, 0.654251789473684
), Point.30 = c(0.651740076, 0.674775824561404), Point.31 = c(0.657197604,
0.679311385964912), Point.32 = c(0.657618572, 0.673946421052632
), Point.33 = c(0.653554616, 0.67093849122807), Point.34 = c(0.648990388,
0.673238403508772), Point.35 = c(0.643885328, 0.669246245614035
), Point.36 = c(0.636234632, 0.670007543859649), Point.37 = c(0.632127604,
0.667657561403509), Point.38 = c(0.631252172, 0.665906228070175
), Point.39 = c(0.637404984, 0.677649561403509), Point.40 = c(0.6451598,
0.679067614035088), Point.41 = c(0.648019716, 0.688604824561403
), Point.42 = c(0.645375244, 0.692729175438596), Point.43 = c(0.647187664,
0.691994543859649), Point.44 = c(0.651923432, 0.681522859649123
), Point.45 = c(0.650062976, 0.674073456140351), Point.46 = c(0.638525956,
0.660092263157895), Point.47 = c(0.627772732, 0.652689456140351
), Point.48 = c(0.615988064, 0.650307087719298), Point.49 = c(0.599147952,
0.651349771929825), Point.50 = c(0.584897698795181, 0.63722649122807
)), class = "data.frame", row.names = c(NA, -2L))
which a subset of the 50 points looks like:
AgeGroup Point.1 Point.2 Point.3 Point.4 Point.5 Point.6 Point.7 Point.8 Point.9 Point.10 Point.11 Point.12 Point.13 Point.14 Point.15 Point.16
1 Old 0.4018994 0.4356104 0.4669511 0.4909977 0.5104751 0.5196141 0.5249242 0.5354482 0.5392982 0.5462890 0.5522868 0.5486441 0.5460290 0.5514643 0.5583580 0.5609581
2 Young 0.4328450 0.4488264 0.4733396 0.5054166 0.5172288 0.5200771 0.5229394 0.5238469 0.5291321 0.5352219 0.5440697 0.5479887 0.5561008 0.5573428 0.5529951 0.5557530
I am having difficulty plotting all columns on one graph, where X is just 1:50 tick marks and Y is the value of each point, color coded by AgeGroup.
I have tried melt, but I dont think thats necessary as it transposes the data and doubles the Point values.
I've tried variations of the following:
ggplot(YaxL, aes(x=1:50,y=YaxL[2:51])) + geom_point()
and
ggplot(YaxL, aes(x = 1:50)) +
geom_line(aes(y = YaxLDF[1,1], colour = "Old")) +
geom_line(aes(y = YaxLDF[2,1], colour = "Young"))
I feel like I'm overthinking this, help appreciated.
Try this approach. You can reshape to long with pivot_longer() and use the separate() function to extract the point position. After that the design of the plot is very practical. I have used the data you shared as YaxL. Always first try to reshape your data and then the plots can be easily built. Here the code:
library(tidyverse)
#Data process and plot
YaxL %>% pivot_longer(-1) %>%
separate(name,c('name','x'),sep='\\.') %>%
mutate(x=as.numeric(x)) %>%
dplyr::select(-name) %>%
ggplot(aes(x=x,y=value,color=AgeGroup,group=AgeGroup))+
geom_point()
Output:
I am trying to plot the time and NDVI for each region on the same plot. I think to do this I have to convert the date column from characters to time and then plot each layer. However I cannot figure out how to do this. Any thoughts?
list(structure(list(observation = 1L, HRpcode = NA_character_,
timeseries = NA_character_), row.names = c(NA, -1L), class = c("tbl_df",
"tbl", "data.frame")), structure(list(observation = 1:6, time = c("2014-01-01",
"2014-02-01", "2014-03-01", "2014-04-01", "2014-05-01", "2014-06-01"
), ` NDVI` = c("0.3793765496776215", "0.21686891782421552", "0.3785652933528299",
"0.41027240624704164", "0.4035578030242673", "0.341299793064468"
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(observation = 1:6, time = c("2014-01-01",
"2014-02-01", "2014-03-01", "2014-04-01", "2014-05-01", "2014-06-01"
), ` NDVI` = c("0.4071076986818826", "0.09090719657570319", "0.35214166081795284",
"0.4444311032927228", "0.5220702877666005", "0.5732370503295022"
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(observation = 1:6, time = c("2014-01-01",
"2014-02-01", "2014-03-01", "2014-04-01", "2014-05-01", "2014-06-01"
), ` NDVI` = c("0.3412131556625801", "0.18815996897460135", "0.5218904976415136",
"0.6970128777711452", "0.7229657162729096", "0.535967435470161"
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
)))
111
First we need to clean your data. The first element in this list is empty
df = df[-1]
Now we need to make a data.frame
df = do.call(rbind, df)
I am going to add a region variable, change the name of NDVI to remove the space,
change ndvi into a numeric vector, and change time into a Date object
library(dplyr)
df = df %>%
mutate(region = factor(rep(1:3, rep(6, 3)))) %>%
rename(ndvi = ' NDVI') %>%
mutate(ndvi = as.numeric(ndvi)) %>%
mutate(time = as.Date(time))
Now we can use ggplot2 to plot the data by region
library(ggplot2)
g = df %>%
ggplot(aes(x = time, y = ndvi, col = region)) +
geom_line()
g
Which gives this plot:
Here's an approach with lubridate to handle dates and dplyr to make the binding of the data.frames easier to understand.
Note that the group names are taken from the names of the list, and since those don't exist in the data you provided, we have to set them in advance.
library(lubridate)
library(ggplot2)
library(dplyr)
names(data) <- 1:3
data <- bind_rows(data, .id = "group")
data$time <- ymd(data$time)
setnames(data," NDVI","NDVI")
data$NDVI <- as.numeric(data$NDVI)
ggplot(data, aes(x=time,y=NDVI,color=Group)) + geom_line()
I have a dataset with mean gene counts for each decade as shown below:
structure(list(decade_0 = c(92.500989948184, 2788.27384875413,
28.6937227408861, 1988.03831525414, 1476.83143096418), decade_1 = c(83.4606306426572,
537.725421951383, 10.2747132062782, 235.380422949258, 685.043600629146
), decade_2 = c(188.414375201462, 2091.84249935145, 17.080858894829,
649.55107199935, 1805.3484565514), decade_3 = c(43.3316024314987,
141.64396529835, 2.77851259926935, 94.7748265692319, 413.248354335235
), decade_4 = c(54.4891626582901, 451.076574268175, 12.4298374245007,
346.102609621018, 769.215535857077), decade_5 = c(85.5621750431284,
131.822699578988, 13.3130607062134, 151.002200923853, 387.727911723968
), decade_6 = c(112.860998806804, 4844.59668489898, 19.7317645111144,
2084.76584309876, 766.375852567831), decade_7 = c(73.2198969730458,
566.042952305845, 3.2457873699886, 311.853982701609, 768.801733767044
), decade_8 = c(91.8161648275608, 115.161700090147, 10.7289451320065,
181.747670625714, 549.21661120626), decade_9 = c(123.31045087146,
648.23694540667, 17.7690326882018, 430.301803845829, 677.187054208271
)), row.names = c("ANK1", "NTN4", "PTPRH", "JAG1", "PLAT"), class = "data.frame")
I would like to plot a line graph with the changes in counts over time for each of >30 genes as shown here in excel.
To do this with ggplot I have to convert it to col1: decade, col2: gene, col3: counts.
My question is, either how to convert my table into this ggplot friendly table, or if there is a better way to produce the plot with a different tool?
Thanks!
One possibility: transpose your data frame, convert rownames to columns, then gather ("make long"). Plotting is then easy.
library(tidyverse)
mydat <- structure(list(decade_0 = c(92.500989948184, 2788.27384875413,
28.6937227408861, 1988.03831525414, 1476.83143096418), decade_1 = c(83.4606306426572,
537.725421951383, 10.2747132062782, 235.380422949258, 685.043600629146
), decade_2 = c(188.414375201462, 2091.84249935145, 17.080858894829,
649.55107199935, 1805.3484565514), decade_3 = c(43.3316024314987,
141.64396529835, 2.77851259926935, 94.7748265692319, 413.248354335235
), decade_4 = c(54.4891626582901, 451.076574268175, 12.4298374245007,
346.102609621018, 769.215535857077), decade_5 = c(85.5621750431284,
131.822699578988, 13.3130607062134, 151.002200923853, 387.727911723968
), decade_6 = c(112.860998806804, 4844.59668489898, 19.7317645111144,
2084.76584309876, 766.375852567831), decade_7 = c(73.2198969730458,
566.042952305845, 3.2457873699886, 311.853982701609, 768.801733767044
), decade_8 = c(91.8161648275608, 115.161700090147, 10.7289451320065,
181.747670625714, 549.21661120626), decade_9 = c(123.31045087146,
648.23694540667, 17.7690326882018, 430.301803845829, 677.187054208271
)), row.names = c("ANK1", "NTN4", "PTPRH", "JAG1", "PLAT"), class = "data.frame")
newdat <- mydat %>% t() %>% as.data.frame() %>% tibble::rownames_to_column('decade') %>%
pivot_longer(-decade, names_to = 'gene', values_to = 'count')
ggplot(newdat) + geom_line(aes(decade, count, color = gene, group = gene))
Created on 2020-02-14 by the reprex package (v0.3.0)
I have a seemingly small challenge, but I can't get to an answer. Here is my minimum working example.
fr_nuke <- structure(list(Date = structure(c(1420070400, 1420074000, 1420077600,
1420081200, 1420084800, 1420088400), class = c("POSIXct", "POSIXt"), tzone = ""),
`61` = c(57945, 57652, 57583, 57551, 57465, 57683),
`3244` = c(72666.64, 73508.78, 69749.17, 67080.13, 66357.65, 66524.13),
`778` = c(2.1133, 2.1133, 2.1133, 2.1133, 2.1133, 2.1133),
fcasted_nuke_temp = c(54064.6099092888, 54064.6099092888, 54064.6099092888,
54064.6099092888, 54064.6099092888, 54064.6099092888),
fcasted_nuke_cons = c(55921.043096775, 56319.5688170977, 54540.4094334057,
53277.340242333, 52935.4411965463, 53014.2244890147)),
.Names = c("Date", "61", "3244", "778", "fcasted_nuke_temp", "fcasted_nuke_cons"),
row.names = c(NA, 6L), class = "data.frame")
series1 <- as.xts(fr_nuke$'61', fr_nuke$Date)
series2 <- as.xts(fr_nuke$fcasted_nuke_temp, fr_nuke$Date)
series3 <- as.xts(fr_nuke$fcasted_nuke_cons, fr_nuke$Date)
grp_input <- cbind(series1,series2,series3)
dygraph(grp_input)
The resulting plot does not show the label of the individual series. Specifying the series with
dygraph(grp_input) %>% dySeries("V1", label = "Label1")
Results in:
Error in dySeries(., "V1", label = "Label1") : One or more of the
specified series were not found. Valid series names are: ..1, ..2, ..3
However, it works if I plot only one series (e.g. series1).
dygraph(series1) %>% dySeries("V1", label = "Label1")
Either set the colnames for the grp_input object, or use merge to construct the column names from the object names.
# setting colnames
require(dygraphs)
require(xts)
grp_input <- cbind(series1, series2, series3)
colnames(grp_input) <- c("V1", "V2", "V3")
dygraph(grp_input) %>% dySeries("V1", label = "Label1")
# using merge
require(dygraphs)
require(xts)
grp_input <- merge(series1, series2, series3)
dygraph(grp_input) %>% dySeries("series1", label = "Label1")