I would like to draw a heatmap for some data set.
Originally, I used the heatmap functions with satisfiying results. Yet, as all my other graphs are done with ggplot2, I tried to do it with ggplot2 as well. Yet, I get weird graphs and I am not sure what the reason is.
I am given a dataset as follows:
> dput(B)
structure(list(`2001` = c(510, 15, 14, 9, 8, 11, 7, 5, -1, -3),
`2002` = c(397, -13, 5, 6, 12, -1, 0, 2, 5, 3),
`2003` = c(323, -6, -2, 1, 0, 6, -5, -2, 1, 4),
`2004` = c(133, -2, 2, -4, 0, 5, 8, -2, 0, 1),
`2005` = c(-100, -8, -6, -2, 1, 2, 2, 3, -6, -5),
`2006` = c(-114, -7, 2, -4, -2, 0, 1, 2, 4, -3),
`2007` = c(-130,-13, 0, 4, -3, -2, -1, 1, 2, 4),
`2008` = c(-38, -10, 4, 0, 3, 4, 2, 0, 0, 1),
`2009` = c(-194, -13, -5, -4, -3, -1, 0, 1, 1, 1),
`2010` = c(-202, -6, 0, -1, -5, -2, -3, -1, 2, -2)),
row.names = 0:9, class = "data.frame")
Now using,
> heatmap(as.matrix(B), Colv = NA, Rowv = NA, col = cm.colors(10))
I obtain the following heatmap, which looks reasonable.
On the other hand, with
C <- B
C$size <- row.names(C)
C <- melt(C, variable.name="year",id=c("size"))
ggplot(data = C) +
geom_tile(aes(x = year, y = size, fill = value)) +
scale_fill_gradientn(colors=cm.colors(10))
I get the following
Why is this? And how can I correct this such that I get a plot similar to the first one?
Scaling is applied in heatmap. So you need to scale your data (according to size, in this instance) to reproduce something similar, such as:
C2 <- C %>% group_by(size) %>% mutate(rescale = scale(value))
ggplot(data = C2) +
geom_tile(aes(x = year, y = size, fill = rescale)) +
scale_fill_gradientn(colors=cm.colors(10))
There are other options, depending on what you are trying to visualise. For example, have a look at limits and values in scale_fill_gradientn.
The issue is that size = 0 has much larger numbers than the other entries. So the second heat map is correct because the rest of the values are very close to zero in respect to the given scale. I would say that the second one is actually correct because you can see that outside of size = 0 the numbers are roughly the same, especially in respect to the large numbers found in that first row. I'm not quite sure how heatmap works, and it doesn't supply a scale, but I think the second graph make sense give the low variability outside of the first row.
Related
I get an error message of only handles two-variable equations, is there any function that makes plotEqn available for 4 variables?
A <- matrix(c(0, 1, 1, -2, 1, 2, -1, 0, 2, 4, 1, -3, 1, -4, -7, -1),
nrow = 4,
ncol = 4,
byrow = T)
b <- c(-3, 2, -2, -19)
Solve(A, b, fractions = TRUE)
plotEqn(A, b)
I am analysing segments of vectors, and for that I need to extract their maximum.
The normal way to do this would be with max(vector, na.rm = TRUE).
However, in some segments the actual maximum of the vector doesn't appear, so it gives the maximum of that segment of the vector.
For this, I want to extract the maximum from the head() function, as it appears here:
library(expss)
nps = c(-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 1)
var_lab(nps) = "Net promoter score"
val_lab(nps) = num_lab("-1 Detractors
0 Neutralists
1 Promoters")
head(nps)
Output:
Detractors -1 Detractors
Neutralists 0 Neutralists
Promoters 1 Promoters
In this case, I would want to extract '1' from the middle column. I have tried turning the output of head() into a dataframe but that just gives the first 5 values of the vector. Any ideas?
If all your values have labels then simplest way is to extract maximum from value labels:
library(expss)
vec = structure(c(6, 5, 5, 4, 5, 5), labels = c(`Missing; Unknown` = -5, `Not asked in survey` = -4, `Not applicable` = -3, `No answer` = -2, `Don´t know` = -1, Left = 1, `2` = 2, `3` = 3, `4` = 4, `5` = 5, `6` = 6, `7` = 7, `8` = 8, `9` = 9, Right = 10), class = "labelled")
max(val_lab(vec))
# 10
If your vector has some values without labels, then you can use unique:
nps = c(-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 1)
var_lab(nps) = "Net promoter score"
# no label for 1
val_lab(nps) = num_lab("-1 Detractors
0 Neutralists")
# nmax = 1 indicate that unique will return values from vector and values from labels
max(unique(nps, nmax = 1))
nps = c(-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 1)
var_lab(nps) = "Net promoter score"
val_lab(nps) = num_lab("-1 Detractors
0 Neutralists
1 Promoters")
max(stack(attr(nps, 'labels'))$values)
[1] 1
This solution was derivative from the answer here: Extract values and labels of labelled data
The columns represent the grade of response of the respondent and the rows are the representation of the groups of ages. The table was generated with a (matrix?), the goal is to (graph? O make graphic) how the different groups of ages behave with the responses
tabla<-matrix(c(0, 0, 0, 1, 0, 0,
1, 0, 0, 0, 9, 0,
9, 1, 1, 5, 22, 0,
18, 1, 3, 1, 27, 1,
25, 7, 4, 6, 22, 3,
20, 2, 0, 0, 18, 1,
6, 2, 0, 2, 22, 0,
2, 0, 1, 1, 0, 4,
12, 0, 0, 5, 6, 0),ncol=6,byrow=TRUE)
colnames(tabla)<-c("No","is a problem","lite preblem","a moderate proble","Big problem","No respond")
rownames(tabla)<-c("16-24.5","24.5-33","33-41.5","41.5-50","50-58.5","58.5-67","67-75.5","75.5-84","No responde")
I think heatmap is a good choice. Here is a solution using the tidyverse package.
library(tidyverse)
tabla2 <- tabla %>%
as.data.frame() %>%
rownames_to_column() %>%
gather(Column, Value, -rowname)
ggplot(tabla2, aes(x = rowname, y = Column, fill = Value)) +
geom_tile() +
scale_fill_gradientn(name = "", colors = terrain.colors(10)) +
scale_x_discrete(name = "") +
scale_y_discrete(name = "")
data(pyrifos)
week <- gl(11, 12, labels=c(-4, -1, 0.1, 1, 2, 4, 8, 12, 15, 19, 24))
dose <- factor(rep(c(0.1, 0, 0, 0.9, 0, 44, 6, 0.1, 44, 0.9, 0, 6), 11))
ditch <- gl(12, 1, length=132)
mod <- prc(pyrifos, dose, week)
plot(mod)
how can I control the placement of the legend in this graph? e.g. if i want it in the bottom right corner instead - for my own data (not shown) the default placement covers the data.
Have you checked the help for plot.prc()?? the legpos parameter is responsible for legend position
Here is you solution
library(vegan)
data(pyrifos)
week <- gl(11, 12, labels=c(-4, -1, 0.1, 1, 2, 4, 8, 12, 15, 19, 24))
dose <- factor(rep(c(0.1, 0, 0, 0.9, 0, 44, 6, 0.1, 44, 0.9, 0, 6), 11))
ditch <- gl(12, 1, length=132)
mod <- prc(pyrifos, dose, week)
plot(mod, legpos="bottomright")
This question already has answers here:
creating "radar chart" (a.k.a. star plot; spider plot) using ggplot2 in R
(5 answers)
Closed 7 years ago.
I am trying to make a Radar plot as in attached image using and ggplot2 ( or any other package in R).This talk about this but my case is different as i am trying to create a spider plot for response data with certain range.
I made a plot using a code as below, but i couldn't figure out howto make this like in the image. Kindly help me with this.
Impcts <- c("system","supply","security","well-being")
present <- c(5,5,3,5)
past <- c(6,6,4,5)
group.names <- c("present", "past")
ddf.pre <- data.frame(matrix(c(rep(group.names[1], 4), Impcts), nrow = 4, ncol = 2), var.order = seq(1:4), value = present)
ddf.pas <- data.frame(matrix(c(rep(group.names[2], 4), Impcts), nrow = 4, ncol = 2), var.order = seq(1:4), value = past)
ddf <- rbind(ddf.pre, ddf.pas)
colnames(ddf) <- c("Group", "Impcts", "var.order", "var.value")
library(ggplot2)
ggplot(ddf, aes(y = var.value, x = reorder(Impcts, var.order),
group = Group, colour = Group))+
coord_polar() +
geom_path() +
geom_point()+
labs(title = "Impacts Analysis").
Here is my attempt.First I drew squares using geom_path(). Then, I drew two polygons on top of the squares using geom_polygon(). Finally I added annotations.
### Draw squares
mydf <- data.frame(id = rep(1:6, each = 5),
x = c(0, 6, 0, -6, 0,
0, 5, 0, -5, 0,
0, 4, 0, -4, 0,
0, 3, 0, -3, 0,
0, 2, 0, -2, 0,
0, 1, 0, -1, 0),
y = c(6, 0, -6, 0, 6,
5, 0, -5, 0, 5,
4, 0, -4, 0, 4,
3, 0, -3, 0, 3,
2, 0, -2, 0, 2,
1, 0, -1, 0, 1))
g <- ggplot(data = mydf, aes(x = x, y = y, group = factor(id)) +
geom_path()
### Draw polygons
mydf2 <- data.frame(id = rep(7:8, each = 5),
x = c(0, 6, 0, -5, 0,
0, 5, 0, -5, 0),
y = c(6, 0, -4, 0, 6,
5, 0, -3, 0, 5))
gg <- g +
geom_polygon(data = mydf2, aes(x = x, y = y, group = factor(id), fill = factor(id))) +
scale_fill_manual(name = "Time", values = c("darkolivegreen4", "brown4"),
labels = c("Past", "Present"))
### Add annotation
mydf3 <- data.frame(x = c(0, 6.5, 0, -6.5),
y = c(6.5, 0, -6.5, 0),
label = c("system", "supply", "security", "well-being"))
ggg <- gg +
annotate("text", x = mydf3$x, y = mydf3$y, label = mydf3$label, size = 3)
ggsave(ggg, file = "name.png", width = 10, height = 9)