问题
Test1 Test2 Test3 Test4 Test5
0.048 0.552 0.544 1.0000 0.604
0.518 0.914 0.948 0.0520 0.596
0.868 0.944 0.934 0.3720 0.340
0.934 0.974 0.896 0.1530 0.090
0.792 0.464 0.096 0.7050 0.362
0.001 0.868 0.050 0.3690 0.380
0.814 0.286 0.162 0.0040 0.092
0.146 0.966 0.044 0.4660 0.726
0.970 0.862 0.001 0.4420 0.020
0.490 0.824 0.634 0.5720 0.018
0.378 0.974 0.002 0.0005 0.004
0.878 0.594 0.532 0.0420 0.366
1.000 1.000 1.000 0.3550 1.000
1.000 1.000 1.000 0.3170 1.000
1.000 1.000 1.000 0.3900 1.000
0.856 0.976 0.218 0.0005 0.001
1.000 1.000 1.000 0.4590 1.000
1.000 1.000 1.000 0.5770 1.000
0.640 0.942 0.766 0.0005 0.320
1.000 1.000 1.000 0.0830 1.000
0.260 0.968 0.032 0.0480 0.300
0.150 0.480 0.108 0.0005 0.008
0.686 0.150 0.400 0.0200 0.060
0.002 0.346 0.004 0.0005 0.098
I would like to make a 24 x 5 Heatmap of my values. I want to set the colors based on my own thresholds. For example, I want all 1.000 values to be black. I would like all values between 0.500-0.999 to be very light blue. I would like all values 0.300-0.499 to be slightly darker blue. All values 0.15-0.299 slightly darker. All values 0.10-0.149 slightly darker, all values 0.5-0.099 slightly darker, all values 0.025-0.049 slightly darker, and all values 0-0.024 the darkest. I have tried using ggplot and heatmap() but I can't figure out the user set colors. Thanks!
回答1:
The scale_color_gradient2()
function from ggplot should work just fine for this. The syntax for this portion of the function would be
p+scale_color_gradient(low='black',high='dodgerblue',mid='blue',midpoint=0.500,
limits=c(0,1))
where p is the plot from your ggplot()+geom_tile() [or whatever heatmap function you're using]
The assumption here is that you don't mind a fluid scale from black to dodger blue, as continuous values will change along that sliding scale. If you want to first create a dummy variable that assigns your target variable to a group based on value then you can use the scale_color_manual()
function with values=c(colors you want go here) and breaks=c(the values of the bins you created go here). Something like this [simplified and not with the total number of buckets you've outlined] --
p<-ggplot(dataset,aes(x=x,y=y,color=binned.var)+geom_tile()+
scale_color_manual(values=c("darkblue","blue","dodgerblue","black"),
breaks=c("bin1","bin2","bin3","bin4"),
labels=c("0-0.024","0.025-0.049","0.05-0.099","0.10-0.149"))
Hope that helps.
来源:https://stackoverflow.com/questions/41602092/r-heatmap-with-controlled-colors-and-thresholds