I am working on a heatmap using heatmap.2 and would like to know if there is anyway to display the values on all heatmap positions. For example for the area representing "1" and rating I would like to display value "43", for "2" and privileges the value 51 and so on.
My sample data is as follows:
rating complaints privileges learning raises critical advance
1 43 51 30 39 61 92 45
2 63 64 51 54 63 73 47
3 71 70 68 69 76 86 48
4 61 63 45 47 54 84 35
Is this what you mean? By providing the data object as the cellnote argument, the values are printed in the heatmap.
heatmap.2(data, # cell labeling
cellnote=data,
notecex=1.0,
notecol="cyan",
na.color=par("bg"))
The answer is just for "For Cell labeling is there anyway not to display values that are 0".
cellnote=ifelse(data==0, NA, data) will work as you want.
In python when using seaborn.heatmap by simply using annot=True, all the values are displayed in the heatmap plot. See the example below:
Related
I am a very new R user and struggling with writing a function.
I would like to write a function for the sample data frame below where the text “Number of students” is printed for each remark category with the corresponding number of students in each category.
student.id midterm final remark
student.1 83 81 Excellent
student.2 52 42 Work Harder
student.3 62 8 Work Harder
student.4 50 44 Work Harder
student.5 86 80 Excellent
student.6 90 1 Not Bad
student.7 73 70 Work Harder
student.8 87 84 Excellent
student.9 55 23 Work Harder
student.10 62 87 Not Bad
student.11 72 78 Work Harder
student.12 70 91 Not Bad
student.13 57 33 Work Harder
student.14 61 43 Work Harder
student.15 60 11 Work Harder
student.16 59 13 Work Harder
student.17 53 26 Work Harder
Here is one way to do this. The function takes the data frame as an argument:
remark_frequency <- function(df){
countTable <- as.data.frame(table(df$remark))
for(i in 1:nrow(countTable)){
printString <- paste("Number of students in",
countTable[i,1],
"category:",
countTable[i,2])
print(printString)
}
}
final.marks
# raj sanga rohan rahul
#physics 45 43 44 49
#chemistry 47 45 48 47
#total 92 88 92 96
This is the matrix I have. Now I want to find the total for each subject separately across respective subject rows and add them as a new column to the above matrix as the 5th column . However my code i.e class.marks.chemistry<- rowSums(final.marks[2,]) keeps producing an error saying
Error saying
rowSums(final.marks[2, ]) :
'x' must be an array of at least two dimensions
Can you please help me solve it. I am very new to R or any form of scripting or programming background.
Do you mean this?
# Sample data
df <- read.table(text =
" raj sanga rohan rahul
physics 45 43 44 49
chemistry 47 45 48 47
total 92 88 92 96", header = T)
# Add column total with row sum
df$total <- rowSums(df);
df;
# raj sanga rohan rahul total
#physics 45 43 44 49 181
#chemistry 47 45 48 47 187
#total 92 88 92 96 368
The above also works if df is a matrix instead of a data.frame.
If you look at ?rowSums you can see that the x argument needs to be
an array of two or more dimensions, containing numeric,
complex, integer or logical values, or a numeric data frame.
So in your case we must pass the entire data.frame (or matrix) as an argument, rather than a specific column (like you did).
Another option would be to use addmargins on a matrix
addmargins(as.matrix(df), 2)
# raj sanga rohan rahul Sum
#physics 45 43 44 49 181
#chemistry 47 45 48 47 187
#total 92 88 92 96 368
I have an map of efficiency data as matrix stored in a text file. With increasing row number data points are missing at the end of the line since there are no data available. When I try to read the data as matrix in R:
mymatrix = as.matrix(read.table(file="~/Desktop/Map_.txt"))
I get the error message that the row 3 (in this case the first row with missing values at the rows end) has not all values expected.
Is there a way to read the map file as matrix even with free spaces (no values) in some lines?
Thank you!
example:
1000 2000 3000 4000 5000
-1 85 75 65 60 58
-2 86 74 64 58 52
-3 83 78 68 59
-4 86 80 72
-5 86 81 71
I like Leaflet package it is interactive, with good basemap and high zooming power. Thus, I want to use leaflet package to produce a travel route. I know map and ggmap packages can be used to produce a map such as this:Example of map to produce
In ggmap, there is gcintermediate function that connects draws multiple lines simultaneously, but I am not sure if similar function is in leaflet package. I tried using leaflet package, but could only use it to connect two points. So, my question is that can leaflet package be used to visualize multiple routes simultaneously?
Below is my attempt:
library(leaflet)
p1=leaflet()%>%
addTiles() %>%
mydf2=data.frame(inter)
#mydf2 is below
lon lat
1 -157.8583 21.3069444
2 -158.1350 20.7322775
3 -158.4096 20.1571716
4 -158.6822 19.5816431
5 -158.9528 19.0057081
6 -159.2216 18.4293823
7 -159.4885 17.8526808
8 -159.7538 17.2756187
9 -160.0173 16.6982106
10 -160.2793 16.1204708
11 -160.5398 15.5424133
12 -160.7988 14.9640518
13 -161.0564 14.3853998
14 -161.3127 13.8064705
15 -161.5677 13.2272769
16 -161.8215 12.6478316
17 -162.0742 12.0681472
18 -162.3258 11.4882361
19 -162.5763 10.9081102
20 -162.8259 10.3277815
21 -163.0745 9.7472619
22 -163.3223 9.1665628
23 -163.5692 8.5856958
24 -163.8154 8.0046721
25 -164.0609 7.4235030
26 -164.3058 6.8421994
27 -164.5501 6.2607724
28 -164.7938 5.6792327
29 -165.0370 5.0975911
30 -165.2798 4.5158583
31 -165.5222 3.9340449
32 -165.7643 3.3521614
33 -166.0060 2.7702183
34 -166.2476 2.1882260
35 -166.4889 1.6061948
36 -166.7301 1.0241352
37 -166.9712 0.4420575
38 -167.2123 -0.1400281
39 -167.4534 -0.7221112
40 -167.6946 -1.3041815
41 -167.9359 -1.8862287
42 -168.1773 -2.4682425
43 -168.4189 -3.0502124
44 -168.6608 -3.6321282
45 -168.9030 -4.2139792
46 -169.1456 -4.7957552
47 -169.3886 -5.3774454
48 -169.6321 -5.9590393
49 -169.8761 -6.5405261
50 -170.1206 -7.1218951
51 -170.3658 -7.7031354
52 -170.6116 -8.2842359
53 -170.8582 -8.8651856
54 -171.1055 -9.4459733
55 -171.3537 -10.0265874
56 -171.6028 -10.6070167
57 -171.8528 -11.1872492
58 -172.1038 -11.7672733
59 -172.3559 -12.3470769
60 -172.6091 -12.9266478
61 -172.8635 -13.5059736
62 -173.1191 -14.0850416
63 -173.3760 -14.6638391
64 -173.6343 -15.2423530
65 -173.8940 -15.8205699
66 -174.1552 -16.3984763
67 -174.4179 -16.9760583
68 -174.6823 -17.5533017
69 -174.9484 -18.1301922
70 -175.2162 -18.7067148
71 -175.4858 -19.2828545
72 -175.7574 -19.8585959
73 -176.0309 -20.4339231
74 -176.3065 -21.0088199
75 -176.5842 -21.5832696
76 -176.8641 -22.1572552
77 -177.1464 -22.7307591
78 -177.4310 -23.3037635
79 -177.7180 -23.8762498
80 -178.0076 -24.4481991
81 -178.2999 -25.0195918
82 -178.5948 -25.5904080
83 -178.8927 -26.1606268
84 -179.1934 -26.7302271
85 -179.4971 -27.2991869
86 -179.8040 -27.8674837
87 179.8859 -28.4350940
88 179.5724 -29.0019940
89 179.2555 -29.5681588
90 178.9350 -30.1335627
91 178.6108 -30.6981794
92 178.2828 -31.2619815
93 177.9509 -31.8249406
94 177.6149 -32.3870276
95 177.2747 -32.9482123
96 176.9302 -33.5084632
97 176.5811 -34.0677479
98 176.2275 -34.6260330
99 175.8690 -35.1832834
100 175.5056 -35.7394632
101 175.1371 -36.2945349
102 174.7633 -36.8484597
addPolylines(data = mydf2, lng = ~lon, lat = ~lat)
This is what I got from my attempt.
my attempt
The example you provided uses Sources and Targets.
You need some sort of 'Source' as lat1 and then 'Target' as lat2.
Then the same for longitude 'Source' as lon1 and 'Target' as lon2.
c(lat1, lon1)
c(lat2, lon2)
There is an example here: http://kateto.net/network-visualization
The flight path map is the last graph on the website, so scroll to the bottom immediately and then work your way up.
Re-frame your data set into Sources and Targets and try and replot.
Here is a similar question with an answer, which explains how to pass gcintermediate into Leaflet:
Adding Curved Flight path using R's Leaflet Package
I am trying to set up a bar chart to compare control and experimental samples taken of specific compounds. The data set is known as 'hydrocarbon3' and contains the following information:
Exp. Contr.
c12 89 49
c17 79 30
c26 78 35
c42 63 3
pris 0.5 0.8
phy 0.5 0.9
nap 87 48
nap1 83 44
nap2 78 44
nap3 73 20
acen1 81 50
acen2 86 46
fluor 83 11
fluor1 68 13
fluor2 79 17
dibe 65 7
dibe1 67 6
dibe2 56 10
phen 82 13
phen1 70 12
phen2 65 15
phen3 53 14
fluro 62 9
pyren 48 11
pyren1 34 10
pyren2 19 8
chrys 22 3
chrys1 21 3
chrys2 21 3
When I create a bar chart with the formula:
barplot(as.matrix(hydrocarbon3),
main=c("Fig 1. Change in concentrations of different hydrocarbon compounds\nin sediments with and without the presence of bacteria after 21 days"),
beside=TRUE,
xlab="Oiled sediment samples collected at 21 days",
space=c(0,2),
ylab="% loss in concentration relative to day 0")
I receive this diagram, however I need the control and experimental samples of each chemical be next to each other allow a more accurate comparison, rather than the experimental samples bunched on the left and control samples bunched on the right: Is there a way to correct this on R?
Try transposing your matrix:
barplot(t(as.matrix(hydrocarbon3)), beside=T)
Basically, barplot will plot things in the order they show up in the matrix, which, since a matrix is just a vector wrapped colwise, means barplot will plot all the values of the first column, then all those of the second column, etc.
Check this question out: Barplot with 2 variables side by side
It uses ggplot2, so you'll have to use the following code before running it:
intall.packages("ggplot2")
library(ggplot2)
Hopefully this works for you. Plus it looks a little nicer with ggplot2!
> df
row exp con
1 a 1 2
2 b 2 3
3 c 3 4
> barplot(rbind(df$exp,df$con),
+ beside = TRUE,names.arg=df$row)
produces: