Using R to plot a stacked bargraph but the legend does not show up, using GridDB as my database.

huangapple go评论76阅读模式
英文:

Using R to plot a stacked bargraph but the legend does not show up , using GridDB as my database

问题

我编写了一个R函数来从GridDB读取数据,然后绘制堆叠条形图。读取GridDB的数据后,我将其存储在一个“tibble”中(不是一个完整的数据框,而是一个简单的数据结构,可以轻松地传递给POST Web API)。绘制堆叠条形图时,条形是可见的,但图例却不可见。我尝试了许多变化,但图例仍然不可见,即使在barplot()函数中明确指定了args.legend参数。堆叠条形图图像已附上。虚拟数据以CSV格式在此处 - https://docs.google.com/spreadsheets/d/1u73_f7VJms0dv1-Qk_ScswB6vUPPNWeSp3xpeLfASgU/edit?usp=sharing 以下是代码中相关的片段。

# 使用GridDB的Web API返回相关数据
qr1 <- GET(url = my_query_url,
  add_headers("Content-Type" = "application/json; charset=UTF-8"),
  config = authenticate("my_user_name", "mypass"),
  body = query_request_body
)

# 将返回的数据复制到R tibble中
my_global_health_data <- as_tibble(content(qr1, "parsed"))

# 绘制条形图
barplot(
  matrix(c(my_global_health_data$Percent, my_global_health_data$otherCol), nrow = 2, ncol = 20, byrow = TRUE),
  main = "Cause of death, by communicable diseases and maternal, prenatal and nutrition conditions",
  names.arg = my_global_health_data$CountryCode,
  xlab = "CountryName",
  ylab = "Percent of Total",
  col = c("blue", "red"),
  args.legend = "bottomright"
)

print("EOP, R and GridDB")

你能告诉我我做错了什么,或者只是贴出一个对你有效并显示图例的barplot()函数吗?

Using R to plot a stacked bargraph but the legend does not show up, using GridDB as my database.

谢谢
Pratik

使用了R语言的barplot函数,但图例不可见。args.legend参数似乎对我不起作用。barplot(H, xlab, ylab, main, names.arg, col, args.legend)

英文:

I wrote an R function to read data from GridDB, and then plot a stacked bar graph.
After reading the data from GridDB, I store it in a "tibble" ( not a full fledged data frame, a
tibble is a simple data structure and can easily be fed to a POST Web API).
When I plot a stacked bar graph, the bars are visible but the legend is not.
I used many variations but the legend is still not visible, even after clearly specifying the args.legend
parameter to the barplot() function.
Stacked Bar graph image is attached.
Dummy data is here in CSV format - https://docs.google.com/spreadsheets/d/1u73_f7VJms0dv1-Qk_ScswB6vUPPNWeSp3xpeLfASgU/edit?usp=sharing
Below is the relevant snippet from my code.

.....
# Use GridDb's web API to return relevant data
qr1 <- GET (url = my_query_url,
add_headers("Content-Type" = "application/json; charset=UTF-8" ) ,
config = authenticate("my_user_name", "mypass"),
body = query_request_body
)
#Copy data returned into an R tibble
my_global_health__data <- qr1
# Plot the bars
barplot( matrix(c(my_global_health__data$Percent, my_global_health__data$otherCol), nrow=2, ncol=20, byrow=TRUE), main="Cause of death, by communicable diseases

and maternal, prenatal and nutrition conditions", names.arg = my_global_health_data$CountryCode , xlab="CountryName", ylab="Percent of Total", col=c("blue","red"),

args.legend="bottomright" )

print(&quot;EOP, R and GridDB&quot;) 
#`

.................... `

Can you give me some idea about what I am doing incorrectly, OR just post a barplot() function that works for you and shows up the legend too.

Using R to plot a stacked bargraph but the legend does not show up, using GridDB as my database.
Thanks
Pratik

Used R language's barplot function, but the legend is not visible. the parameter args.legend does not seem to work for me. barplot(H, xlab, ylab, main, names.arg, col, args.legend)

答案1

得分: 1

以下是翻译好的部分:

"Since you didn't provide the data directly, I just created some dummy data.
I used ggplot2 and did not have any issues, after shaping the data into long format."

"由于您没有直接提供数据,我只是创建了一些虚拟数据。我使用了ggplot2,在将数据整理成长格式后没有遇到任何问题。"

英文:

Since you didn't provide the data directly, I just created some dummy data.
I used ggplot2 and did not have any issues, after shaping the data into long format.

library(tidyverse)
library(googlesheets4)

set.seed(123)

name &lt;- c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;)
cause1 &lt;- sample(55:98, 4)
cause2 &lt;- sample(55:98, 4)

df &lt;- tibble(name, cause1, cause2)

df %&gt;%
  pivot_longer(cols = 2:3, names_to = &quot;cause&quot;, values_to = &quot;perc&quot;) %&gt;%
  ggplot(aes(x=name, y=perc, fill=cause))+
  geom_col()

Using R to plot a stacked bargraph but the legend does not show up, using GridDB as my database.<!-- -->

<sup>Created on 2023-04-17 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年4月17日 20:44:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定