美丽的ggplot超过10种颜色

时间:2018-05-28 14:14:30

标签: r ggplot2 colors

我从这个ggplot开始:

library(ggplot2) 
library(reshape2) 

data <- read.delim(textConnection("
Groups Time_1 Time_2 Time_3 Time_4
A 63.8 60.6 65.2 66.6
B 9.4 14.0 11.1 7.5
C 7.4 8.5 6.9 8.6
D 13.9 8.4 7.9 11.4
E 1.4 3.8 5.0 1.5
F 0.2 0.2 0.2 0.2
G 1.8 2.5 1.8 2.7
H 1.0 0.9 0.9 1.1
I 45.0 42.0 49.0 38.0
J 1.0 1.1 0.9 0.5
K 0.1 2.0 6.5 1.0
L 0.5 0.9 0.5 0.2
M 0.2 0.2 0.1 0.3"), sep = " ", header = T)


data_melt <- melt(data, id.var = "Groups")
data_melt$value <- as.numeric(data_melt$value)

ggplot <- ggplot(data=data_melt, aes(x=variable, y=value, group = Groups, color = Groups)) + geom_point(size = 1) + geom_line(size = 1)
ggplot

但是,正如您在图表中看到的那样,颜色代码并不是最佳的,并且很难区分哪种颜色与哪些组相对应,即使曲线分离得很好:

enter image description here

不幸的是,我总是有10到13组,而且我花了一些时间看R调色板,但通常它们太小了。

因此,我正在寻找有关如何使用更好的颜色代码来改进图表的想法,任何线索都会有所帮助!

3 个答案:

答案 0 :(得分:2)

http://colorbrewer2.org有一些不错的调色板,最多12种颜色(将数据的性质设置为&#39;定性&#39;),它很容易将十六进制值传输到R.你和#39;尽管如此,我们必须对你的第13个值进行调整。

答案 1 :(得分:2)

如果你拥挤的数据,你也可以考虑像这样:

function functForward{
    var myRPM = document.getElementById('slider').value;
    var myAng = document.getElementById('slider2').value;

    //functForward code
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          document.getElementById("demo").innerHTML = this.responseText;
        }
      };
      var request = "?drive=1" + "&rpm=" + myRPM + "&ang=" + myAng;
      console.log(request);
      xhttp.open("GET", request, true);
      xhttp.send();
}

function functStopDrive(){
    var myRPM = document.getElementById('slider').value;
    var myAng = document.getElementById('slider2').value;

    //funcstopdrive code
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          document.getElementById("demo").innerHTML = this.responseText;
        }
      };
      var request = "?drive=0" + "&rpm=" + myRPM + "&ang=" + myAng;
      console.log(request);
      xhttp.open("GET", request, true);
      xhttp.send();
}

var addEvent = document.addEventListener ? function(target, type, action) {
  if (target) {
    target.addEventListener(type, action, false);
  }
} : function(target, type, action) {
  if (target) {
    target.attachEvent('on' + type, action, false);
  }
}

addEvent(document, 'keydown', function(e) {
  e = e || window.event;
  var key = e.which || e.keyCode;
  if (key === 87) {
    functForward();
  }
});
addEvent(document, 'keyup', function(e) {
  e = e || window.event;
  var key = e.which || e.keyCode;
  if (key === 87) {
    functStopDrive();
  }
});

<script 
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"> 
</script>
<div class="col-sm-4">
<button id="Forward" type="button" class="btn btn-success btn-lg btn-block" 
ng-mousedown="functForward();" ng-mouseup="functStopDrive();">Forward</button>
</div>

<div id="slidecontainer">
 <input type="range" ng-model="myRPM" min="0" max="8000" id="slider" step="500">

 <input type="range" ng-model="myAng" min="-60" max="60" id="slider2" step="5">
</div>

enter image description here

答案 2 :(得分:1)

如果您使用ggfocus扩展程序,则可以使用少量颜色突出显示最重要的级别,并使其他所有级别变为灰色。如果您只关注级别的子组,则可以减少色阶范围,而不会丢失完整数据的信息。请参阅以下示例:

enter image description here