从ggplot2中的图例中删除标签

时间:2015-08-21 13:29:28

标签: r ggplot2 legend

我有下面的数据框,如图所示。如何将图例中显示的值限制为仅前三个?换句话说,我希望它只显示" A"," B"和" C"。

    public void addData(User data) {
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        if (conn == null || conn.isClosed()) {
            throw new RuntimeException("Could not open connection");
        }
        String query="INSERT INTO USER "
                + "(ID, NAME, PASSWORD, ENABLED, REQUIRE_RESET)"
                + "VALUES (?, ?, ?, ?, ?);";

        PreparedStatement ps = conn.prepareStatement(query);
        // "ID" column is INTEGER, and partition field in UserData is int.
        ps.setString(1, data.getId());
        ps.setString(2, data.getName());
        ps.setString(3, data.getPassword());
        // "ENABLED" column is INTEGER, and enabled field in UserData is boolean.
        ps.setInt(4, data.isEnabled()? 1:0); 
        ps.setString(5, data.isRequireReset()?"Y":"N");
        if (ps.executeUpdate() < 1) {
            throw new RuntimeException("Could not save User, executeUpdate() returned a value < 1");
        }

    } Catch (SQLException e) {
        throw new DataLayerException("Could not save the User Data.", e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }
}

1 个答案:

答案 0 :(得分:5)

一开始很困惑,但你想展示前三名,所以其他名字不需要传奇。你走了:

p <- ggplot(graph_table[1:10,], aes(x=rank, y=percs,   
                                    fill=names))+geom_bar(stat="identity")
p <- p+geom_text(aes(label=sums_str, y=(sums+4)), size=4)
p + scale_fill_discrete(breaks=c("A","B","C"))

enter image description here