如何使用geom_circle函数绘制圆

时间:2018-08-27 20:14:44

标签: r ggplot2 ggforce

我的目标是使用ggplot2和ggforce软件包的组合来绘制NBA篮球场的尺寸/线条。我已经使用+ geom_segment()层成功绘制了线段(边线,罚球线等),但是使用+ geom_circle()和+ geom_arc()函数来绘制圆和圆弧(三点线,半场圆等)

我的代码如下,其中对象“样本”只是镜头的数据帧,具有x和y坐标:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.net.URISyntaxException;
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.*;


public class MainActivity extends AppCompatActivity {

private Socket socket;
private static final String URL = "https://coincap.io";

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

    try {
        socket = IO.socket(URL);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            socket.emit("global");
            socket.disconnect();
        }
    }).on("global", new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            Object[] objects = args;
            Log.i("TAG", objects.toString());

        }
    }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
        @Override
        public void call(Object... args) {

        }
    });
    socket.connect();
    if (socket.connected()) {
        Toast.makeText(MainActivity.this, "CONNECTED TO SERVER", Toast.LENGTH_SHORT).show();
        Log.i("TAG", "CONNECTED");
    } else {
        Toast.makeText(MainActivity.this, "CANNOT CONNECT TO SERVER", Toast.LENGTH_SHORT).show();
    }
}
}

当我添加时:

ggplot(sample, aes(shot_x, shot_y)) +
geom_point(color = "red", alpha = .2) +
geom_segment(aes(x = 0, xend = 94, y = 0, yend = 0)) +
geom_segment(aes(x = 0, xend = 94, y = 50, yend = 50)) +
geom_segment(aes(x = 0, xend = 0, y = 0, yend = 50)) +
geom_segment(aes(x = 94, xend = 94, y = 50, yend = 0)) +
geom_segment(aes(x = 0, xend = 14, y = 3, yend = 3)) +
geom_segment(aes(x = 80, xend = 94, y = 3, yend = 3)) +
geom_segment(aes(x = 0, xend = 14, y = 47, yend = 47)) +
geom_segment(aes(x = 80, xend = 94, y = 47, yend = 47)) +
geom_segment(aes(x = 47, xend = 47, y = 0, yend = 50)) +
geom_segment(aes(x = 0, xend = 19, y = 19, yend = 19)) +
geom_segment(aes(x = 0, xend = 19, y = 31, yend = 31)) +
geom_segment(aes(x = 75, xend = 94, y = 19, yend = 19)) +
geom_segment(aes(x = 75, xend = 94, y = 31, yend = 31)) +
geom_segment(aes(x = 19, xend = 19, y = 19, yend = 31)) +
geom_segment(aes(x = 75, xend = 75, y = 19, yend = 31)) +
geom_segment(aes(x = 4, xend = 4, y = 22, yend = 28)) +
geom_segment(aes(x = 90, xend = 90, y = 22, yend = 28)) +
coord_fixed(ratio = 1)

(应该在半场上画一个圆),可视化中没有圆出现,并且结果包括初始图形(线段和拍摄点),以及所有数据点的副本,但偏移向上和向右。要明确的是,没有错误发生,只是结果不是我想要的。

此外,当我完全删除geom_point()层并启动类似以下代码时:

+ geom_circle(aes(x0 = 47, y0 = 25, r = 6))

然后,我可以成功添加geom_circle()层。但是,我需要能够添加圆并还包括数据点。

知道为什么会这样,还是我做错了什么?谢谢!

1 个答案:

答案 0 :(得分:4)

不知道为什么,但是在inherit.aes = FALSE调用中添加geom_circle可以解决此问题。

# generate sample data
sample = data.frame(shot_x = c(10, 20), shot_y = c(30, 40))
ggplot(sample, aes(shot_x, shot_y)) +
  # ... all your segment lines
  coord_fixed(ratio = 1) +
  geom_circle(aes(x0 = 47, y0 = 25, r = 6), inherit.aes = FALSE)

enter image description here