在x,y散点图中使用直接标记

时间:2015-09-11 11:13:11

标签: r ggplot2

我在找到在x,y-scatterplot中使用直接标记的最佳方法时遇到了问题。

String permissions[] = { "email","user_birthday","public_profile"};
fbLogin = (LoginButton) findViewById(R.id.login_button);
fbLogin.setReadPermissions(permissions);

@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken(); 
accessTokenTracker.startTracking();
Profile pf=Profile.getCurrentProfile();
Log.e("Permissions...", ""+AccessToken.getCurrentAccessToken().getPermissions());   

//AccessToken.getCurrentAccessToken().getDeclinedPermissions();
GraphRequest request = GraphRequest.newMeRequest(accessToken,
        new GraphRequest.GraphJSONObjectCallback() {
            String email;
            String firstName;
            String lastName;
            String gender;
            String country;
            String dob, name, picPath, id;

            @Override
            public void onCompleted(JSONObject object,
                    GraphResponse response) {

                try {

                    id = object.getString("id");                        
                    name = object.getString("name");
                    firstName = object.getString("first_name");
                    lastName = object.getString("last_name");
                    gender = object.getString("gender");
                    country = object.getString("address");
                    picPath = Utility.getFbProfileurl(id);
                    Log.e(" birthday..", "..."+object.getString("user_birthday"));
                    dob = object.getString("user_birthday");

                } catch (JSONException e) {
                     e.printStackTrace();
                }
            }



Bundle parameters = new Bundle();
parameters
        .putString("fields",
                "name,email,address,first_name,last_name,gender,location,birthday,locale");

request.setParameters(parameters);
request.executeAsync();

}

虽然这比在geom_text()行中使用vjust / hjust要好得多,但它仍然存在一些问题:

enter image description here

例如,最左边三角形中的下部标签不必要地绘制在该区域上,并且绿色三角形中的一些标签(例如R53,R52)应放置在该区域之外。我在directlabels-package中尝试了很多选项,但是到目前为止,smart.grid是最好的方法。除了使用photoshop之外,还有什么可以改进标签的吗?

这是我的数据:

library(ggplot2)
library(directlabels)
p1 <- ggplot()+
        geom_point(data=sites, aes(X, Y, col=Treatment), alpha=1,show_guide=FALSE) +
        geom_polygon(data = hulls, aes(X, Y, colour=Treatment, fill=Treatment), lty="dashed", alpha = 0.1, show_guide=FALSE) +
        theme_bw() +
        #geom_text(data=sites, aes(X,Y, label=Sample, color=Treatment), size=2, show_guide=FALSE) +
        theme(axis.line = element_line(colour = "black"),
              panel.grid.major = element_blank(),
              panel.grid.minor = element_blank(),
              #panel.border = element_blank(),
              panel.background = element_blank()) +
        coord_fixed() +
        annotate("text", x=-0.8, y=-0.55, label="Stress = 0.102")

p2 <- p1 +  geom_dl(data=sites, aes(X,Y,label=Sample, colour=Treatment, list( cex = 0.6)), method="smart.grid", show_guide=FALSE)
p2

1 个答案:

答案 0 :(得分:1)

directlabels并不是真的用于标记散点图上的各个点(即NP-hard,https://en.wikipedia.org/wiki/Automatic_label_placement

话虽如此,您可能想尝试编写自己的定位方法:

your.method <- function(point.df, ...){
  print(point.df)
  browser()
  label.df <- your_label_computation_function(point.df) 
  label.df
}
p2 <- p1 +  geom_dl(data=sites, aes(X,Y,label=Sample, colour=Treatment, list( cex = 0.6)), method="your.method", show_guide=FALSE)