将标签放在符号内

时间:2016-07-13 07:08:36

标签: r ggplot2

我想制作一个包含绘图符号内标签的x-y图。

我的第一次尝试就是:

dx <- data.frame(x=c(1,2,3),y=c(3,5,8),z=c(1,10,20))
ggplot(data=dx, aes(x=x,y=y,label=as.factor(z)))+
geom_point(color="black",shape=1,size=6) + 
geom_text(hjust = 1, nudge_x = 0.04)

结果几乎没问题,但标签(z)不在绘图符号中居中。这可能与geom_text()中的参数有关。我必须选择哪些值才能使数字(z)在圆圈中居中?

1 个答案:

答案 0 :(得分:2)

我们可以使用vjustggplot(data = dx, aes(x = x, y = y, label = as.factor(z))) + geom_point(color = "black", shape = 1, size = 6) + geom_text(hjust = 0.5, vjust = 0.5) here is a good post on how to use)进行调整:

ggplot(data = dx, aes(x = x, y = y, label = as.factor(z))) +
  geom_label()

或者我们可以使用geom_label (ggplot2_2.1.0),它会为我们提供带有圆形边缘的矩形,里面有标签。

@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');

.dropdown-menu-fixed {
  margin: 0;
  padding: 0;
  height: auto;
  background-color: #fff;
}

.dropdown-menu-fixed > ul {
  position: relative;
  margin: 1rem 0 0;
  padding: 0 0 1rem 0;
  min-height: 1px;
  float: none;
  width: 100%;
}

.dropdown-menu-fixed > ul li {
  padding: 3px 20px;
}

.dropdown-menu-fixed a {
  display: block;
  text-decoration: none;
}

.dropdown-header {
  font-weight: bold;
  font-size: 13px;
}

@media (min-width: 768px) {
  .dropdown:hover .dropdown-menu {
    display: block;
    margin-top: 0;
  }
  .dropdown-menu-fixed {
    position: fixed;
    top: 50px;
    width: 100%;
    background-color: #fff;
  }
  .dropdown-menu-fixed > ul {
    position: relative;
    margin: 1rem 0 0;
    padding: 0 0 1rem 60px;
    min-height: 1px;
    float: left;
    width: 33.33333333%;
  }
}
相关问题