geom_histogram使用比例而不是计数

时间:2017-07-10 02:37:32

标签: r ggplot2 dplyr

我有一个多年来两种鱼的长度测量数据框。 dput的输出可以在此处的链接中访问

https://drive.google.com/file/d/0BzArRBVtzxttdUtaZWVoNUwzTFU/view?usp=sharing

修改

由于下面访问数据的问题,我添加了另一个指向csv文件的链接。希望这可以解决问题

https://drive.google.com/open?id=0BzArRBVtzxttZ2RlcDNKdUFERk0

结束编辑

然后我创建了每个物种和年份的长度频率直方图。如果数据以fish_data读入,则以下代码将是可重现的

library(dplyr) library(ggglot2) library(scales)

colour_data <- filter(fish_data, Length >= 50 & Length <= 100)

ggplot(fish_data, aes(x = Length)) + geom_histogram(breaks = seq(0, 700, by = 50), colour = "black") + geom_histogram(data = colour_data, breaks = seq(0, 700, by = 50), fill = "red") + scale_x_continuous(breaks = pretty_breaks(n=15)) + facet_grid(Year~Species) + theme_grey() + labs(y = "Frequency caught\n", x = "\nLength (cm)") + theme(axis.text.x = element_text(angle = 45, hjust = 1))

但是我现在需要将y轴显示为比例而不是总计数。因此,例如,每个小平面的每个50厘米箱的高度应绘制为鱼总数的比例。然后每个方面的总计数加起来为100.

我无法理解如何制作这个新图表。我想我需要操纵原始数据框,但我不确定这看起来如何。

1 个答案:

答案 0 :(得分:0)

如果这是你想要的,请告诉我:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Include these 2 lines ONLY if need to use Toolbar from layout xml as Action Bar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //Add back navigation in the title bar
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //
        //Other works to be done in onCreate.....
        //
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            //Title bar back press triggers onBackPressed()
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    //Both navigation bar back press and title bar back press will trigger this method
    @Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() > 0 ) { 
            getFragmentManager().popBackStack(); 
        } 
        else { 
            super.onBackPressed(); 
        }
    }
}

enter image description here