Skip to content

3.6玫瑰图绘制的R代码会报错,请问怎么解决呢 #7

Open

Description

@dsfefgffd

mydata <- data.frame( a=c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"),
b=c(50, 60, 70, 20,90,110,30))
myAngle <-seq(-20,-340,length.out =7)
ggplot(mydata) +
geom_bar(aes(x=a, y=b),width = 1,stat="identity",
colour = "black",fill="#F8766D") +
geom_text(aes(x=a,y = b-8,label = b),color="white") +
coord_polar(theta = "x",start=0) +
ylim(c(0,120))+
theme_light()+
theme( panel.background = element_blank(),
panel.grid.major = element_line(colour = "grey80",size=.25),
axis.text.y = element_text(size = 12,colour="black"),
axis.line.y = element_line(size=0.25),
axis.text.x=element_text(size = 13,colour="black",angle = myAngle))

Error in if (0 <= angle & angle < 90) { : the condition has length > 1
In addition: The warning message:
Vectorized input to element_text() is not officially supported.
ℹ Results may be unexpected or may change in future versions of ggplot2.

Activity

  1. IvanYan2018 commented on Sep 8, 2023

    @IvanYan2018

    可以用geom_text(aes(x=a,y = 120,label = a,angle = myAngle),color="black",size = 5) 替换x轴坐标标签,还需要隐去原来的x轴标签显示。
    ……
    ggplot(mydata) +
    geom_bar(aes(x=a, y=b),width = 1,stat="identity",
    colour = "black",fill="#F8766D") +
    geom_text(aes(x=a,y = b-8,label = b,angle = myAngle),color="white") +
    geom_text(aes(x=a,y = 120,label = a,angle = myAngle),color="black",size = 5) +
    coord_polar(theta = "x",start=0) +
    ylim(c(0,120))+
    theme_light()+
    theme( panel.background = element_blank(),
    panel.grid.major = element_line(colour = "grey80",size=.25),
    axis.text.y = element_text(size = 12,colour="black"),
    axis.line.y = element_line(size=0.25),
    axis.text.x = element_blank())
    ……

  2. chenwenqi228 commented on Jan 13, 2024

    @chenwenqi228

    问题主要出在最后一行angle=myAngle(Monday这些标签旋转角度),把myAngle换成数字就可以了。我的命令改成:
    ggplot(mydata) +
    geom_bar(aes(x=a, y=b),width = 1,stat="identity",
    colour = "black",fill="#F8766D") +
    geom_text(aes(x=a,y = b-8,label = b),color="white") +
    coord_polar(theta = "x",start=0) +
    ylim(c(0,120))+
    theme_light()+
    theme( panel.background = element_blank(),
    panel.grid.major = element_line(colour = "grey80",size=.25),
    axis.text.y = element_text(size = 12,colour="black"),
    axis.line.y = element_line(size=0.25),
    axis.text.x=element_text(size = 10,colour="black",angle = 0)) #字号缩小一点,标签水平放置
    image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions