以组嵌套的方式绘制多组线

我想绘制(用ggplot)多组线,其中多组线嵌套在组内,即组1由两条独立的线组成,组2由两条独立的线组成,等等,但是这些线应具有相同的颜色跨群体。我知道我可以使用循环遍历各个组,但我想避免这种情况。结果应类似于此(忽略粗体行)。

以组嵌套的方式绘制多组线

此代码产生一个示例数据集。

y <- rep(NA,60)
y[1] <- rnorm(1,0.5)
for (t in 2:60){
  y[t] <- rnorm(1,y[t - 1],0.5)
}
n <- 2

data <- as.data.frame(cbind(rbind(cbind(rep(seq(1,30),n),sort(rep(seq(1,30)),y),cbind(rep(seq(1,y)),3),30)))         )
colnames(data) <- c("t","n","y","group")
data$y <- data$y + rnorm(3 * 30 * 2,0.1)
yzhwyf 回答:以组嵌套的方式绘制多组线

我认为您的问题的答案已在这篇文章中得到解决:Using geom_line with multiple groupings

因此您可以根据数据使用public class Listing extends AppCompatActivity implements TaskAdapter.ItemClicked { RecyclerView recyclerView; RecyclerView.Adapter myAdapter; RecyclerView.LayoutManager layoutManager; ArrayList<Task> tasks; DatabaseHelper myDB; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_listing); recyclerView = findViewById(R.id.list); recyclerView.setHasFixedSize(true); // setting layout maneger layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); myDB = new DatabaseHelper(this); Cursor data = myDB.getListContents(); if (data.getCount() == 0) { Toast.makeText(Listing.this,"no elements",Toast.LENGTH_SHORT).show(); } else { while (data.moveToNext()) { Task task = new Task(data.getString(1),data.getString(2)); tasks.add(task); myAdapter = new TaskAdapter(this,tasks); recyclerView.setAdapter(myAdapter); } } } 中的interaction,代码如下:

ggplot

您应该得到想要的图形。 enter image description here

本文链接:https://www.f2er.com/3141629.html

大家都在问