使用ifelse时,使用ggplot2的3个变量(混合)的散点图在r中给出了错误的结果

时间:2017-04-24 09:40:37

标签: r ggplot2

enter image description here

尝试使用ggplot2绘制三个不同变量的散点图。我希望每个变量都有自己的图标绘图形状和颜色。但是,代码给出了两个树形图是完全相同的,这是错误的!

###################################################################
df = data.frame(
  sh_of = ifelse(U < 0.3, "Gumbel", ifelse( U < 0.5, "Clayton", "Frank")),
  x = X[, 1],
  y = X[, 2],
  z = X[, 3]
)

ggplot(df, aes(x = x, y = y,z=z, colour = sh_of, shape = sh_of)) +
  geom_point()+scale_shape_manual(values=c(12,25,14)) + ggtitle("")+theme(plot.title = element_text(family = "Trebuchet MS", color="black", face="italic", size=15, hjust=0.5))

此代码与此处使用的代码类似1How to get a scatter plot of mixture data with different shape and colour for each distribution? 1。但是,它只适用于两个维度。

<html>
<head>
<script
  src="https://code.jquery.com/jquery-3.2.1.js"
  integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
  crossorigin="anonymous"></script>
<script type="text/javascript">

 $(document).ready(function() {

    $("#display").click(function() {                

        $.ajax({    //create an ajax request to load_page.php
          type: "GET",
          url: "display.php",             
          dataType: "html",
          data: {
              "some-var": "some-value"
          },            
          success: function(response){                    
              $("#responsecontainer").html(response);
          }

        });

    });

});
</script>
</head>
<body>

<form>
  <select id="city" name="city">
  <option value="">City:</option>
  <option value="glasgow">Glasgow</option>
  <option value="london">London</option>
  </select>
  <input type="button" id="display" value="Display All Data" />
</form>
<br>
<div id="responsecontainer">&nbsp;</div>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

ggplot2忽略了z美学,并且由于某种原因添加了facet_wrap(~sh_of)。如果您确实想使用ggplot2,则可以对z维度进行分区,并使用facet_wrap(~z)绘制“切片”。但要获得一个实际的3D情节,你需要另一个包 - 我建议plotly3d scatterplot documentation)或rglplot3d function