ggplot和ggplotly之间的图例位置有何不同?

时间:2019-01-30 23:14:21

标签: r ggplot2 legend ggplotly

我发现ggplot和ggplotly中的同一图表之间有一个有趣而奇怪的区别

public class TimerFragment extends Fragment {

    public TimerFragment() { }

    private static int ONE_MINUTE_MILLIS = (int) TimeUnit.MINUTES.toMillis(1);

    private ProgressBar progressBar;
    private ObjectAnimator animator;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_timer, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        progressBar = view.findViewById(R.id.progressBar);
        progressBar.setMax(ONE_MINUTE_MILLIS);
        progressBar.setProgress(ONE_MINUTE_MILLIS);
        view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startStopTimer();
            }
        });
    }

    private void startStopTimer() {
        if(animator == null) {
            animator = ObjectAnimator.ofInt(progressBar, "progress", ONE_MINUTE_MILLIS, 0);
            animator.setDuration(ONE_MINUTE_MILLIS);
            animator.setInterpolator(new LinearInterpolator());
            animator.start();
        } else {
            animator.cancel();
            animator = null;
            progressBar.setProgress(ONE_MINUTE_MILLIS);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        if(animator != null) {
            animator.cancel();
            animator = null;
        }
    }
}

对于ggplot,它看起来很完美,底部带有图例标题

enter image description here

但是当我用ggpotly()包装它时,图例开始表现不同

enter image description here

我的问题-我想要第一个ggplotly格式的图表,但无法解决此问题,底部的图例不起作用。想法?

谢谢!

1 个答案:

答案 0 :(得分:1)

在一些R专家的帮助下,很快就解决了。

添加了此

ggplotly(income_gap_chart) %>% layout(legend = list(orientation = "h", x = 0.4, y = -0.2))

结果:enter image description here

谢谢!