在ggplot2中自定义facet_grid中的ymax

时间:2015-12-03 11:12:03

标签: r plot ggplot2 facet-wrap

我正在使用facet_grid绘制我的数据。我有三个网格图,我想修复两个图的ymax(或ylimit)。目前,我正在使用以下代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class DisableSortingTest {
  private static String[] columnNames = {"ID", "NAME", "SALARY"};
  private static Object[][] data = {
    {1, "abcd", 2000},
    {2, "xyz",  1800},
    {3, "ijkl", 4600},
    {4, "pqrs", 3400},
    {5, "efgh", 5000}
  };
  private final DefaultTableModel model = new DefaultTableModel(data, columnNames) {
    @Override public Class<?> getColumnClass(int column) {
      return getValueAt(0, column).getClass();
    }
  };
  private final JCheckBox checkBox = new JCheckBox("filter");
  private final RowFilter<TableModel, Integer> filter = new RowFilter<TableModel, Integer>() {
    @Override public boolean include(Entry<? extends TableModel, ? extends Integer> entry) {
      return "pqrs".equals(entry.getModel().getValueAt(entry.getIdentifier(), 1));
    }
  };
  private final JTable table = new JTable(model);
  private final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model) {
    @Override public boolean isSortable(int column) {
      return getRowFilter() == null;
    }
  };
  public JComponent makeUI() {
    table.setRowSorter(sorter);
    checkBox.addActionListener(new ActionListener() {
      @Override public void actionPerformed(ActionEvent e) {
        JCheckBox c = (JCheckBox) e.getSource();
        sorter.setRowFilter(c.isSelected() ? filter : null);
        sorter.setSortKeys(null);
      }
    });
    JPanel p = new JPanel(new BorderLayout());
    p.add(checkBox, BorderLayout.NORTH);
    p.add(new JScrollPane(table));
    return p;
  }
  public static void main(String... args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new DisableSortingTest().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

输出图是:

enter image description here

我想从0-1修正前两个(hpanom和lofanom)图的范围,我不关心第三个图。这是因为前两个表示概率,因此范围总是固定的,而第三个表示我不知道限制的值。

这里我附加了我的整个数据集,因此可以轻松复制案例

f <- ggplot(data=newmel,aes(x=timestamp,y=value,ymin=0,ymax=value))+facet_grid(variable~., scales = "free_y")+
  theme(axis.title.x=element_blank(),axis.title.y=element_blank())
f1 <- f + geom_linerange(subset=.(variable=="hpanom"))  # require(plyr) for dot function
f2 <- f1 + geom_linerange(subset=.(variable=="lofanom"))  
f3 <- f2 + geom_line(subset=.(variable=="power")) 
f3

1 个答案:

答案 0 :(得分:1)

这有点像黑客攻击,但您可以使用size=0 y=1来强制在linerange两个plyr图上限制为1,从而绘制一个“不可见”点,如下所示。我还删除了使用 f <- ggplot(data=newmel,aes(x=timestamp,y=value,ymin=0,ymax=value))+facet_grid(variable~., scales = "free_y") f <- f + theme(axis.title.x=element_blank(),axis.title.y=element_blank()) f <- f + geom_linerange(data=subset(newmel, variable %in% c("hpanom","lofanom"))) # plot invisible point ( size=0) to set upper limit of y axis to 1 f <- f + geom_point(data=subset(newmel, variable %in% c("hpanom","lofanom")), aes(x=min(timestamp), y=1), size=0) f <- f + geom_line(data=subset(newmel, variable=="power")) f 的需要。

ggplot 2.0

更新ggplot 2.0

size=0中,设置colour = NA不再使该点不可见。相反,请使用library(ggplot2) f <- ggplot(data=newmel,aes(x=timestamp,y=value,ymin=0,ymax=value))+facet_grid(variable~., scales = "free_y") f <- f + theme(axis.title.x=element_blank(),axis.title.y=element_blank()) f <- f + geom_linerange(data=subset(newmel, variable %in% c("hpanom","lofanom"))) # plot transparent point ( colour = NA) to set upper limit of y axis at 1 f <- f + geom_point(data=subset(newmel, variable %in% c("hpanom","lofanom")), aes(x=timestamp[1], y=1), colour=NA ) f <- f + geom_line(data=subset(newmel, variable=="power")) f 使其透明。新解决方案

let end: Date = new Date();
    let h: string = "13";
    let m: string = "20";
    moment(end).set({ hour: parseInt(h, 10), minute: parseInt(m, 10) });