facet_grid,绘图中出现额外的意外NA线。

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

facet_grid, extra unintended NA line in plot

问题

我已经翻译了您提供的代码的一部分,以下是翻译好的部分:

我正在使用`ggplot2`的`facet_grid`函数创建一个图表,其中每个面板都包括5个变量的散点图和连接线。
我的数据可以在[这里][4]找到。
我使用了来自引用链接的以下代码来创建下面的输出。

library(tidyverse)
library(ggplot2)
p <- 2.5    # 点大小
lw <- 0.7  # 线宽度

plot_data %>%
  mutate(across(where(is.character),
                ~parse_number(.x, locale = locale(decimal_mark = ",")))) %>%
  pivot_longer(-c(Ausfallrate, AnzahlJ, AnzahlI),
               names_to = "Type",
               values_to = "PFC") %>%
  mutate(Type = factor(Type, levels = c("PFC_RIMCA", "PFC_MIMCA", "PFC_Hot", "PFC_Mode"))) %>%
  ggplot(aes(x = Ausfallrate, y = PFC,
             group = Type, color = Type,
             shape = Type)) +
  geom_point(size = p) +
  geom_line(linewidth = lw) +
  facet_grid(rows = vars(AnzahlI), cols = vars(AnzahlJ),
             labeller = labeller(
               AnzahlJ = c(`4` = "J=4", `7` = "J=7", `10` = "J=10"),
               AnzahlI = c(`100` = "I=100", `500` = "I=500", `1000` = "I=1000")
             )) +

scale_color_manual(values = c("blue", "black", "red", "orange"),
                     labels = c("RIMCA", "MIMCA", "Hotdeck", "Mode")) +
  scale_shape_manual(values = c(15, 16, 17, 18),
                     labels = c("RIMCA", "MIMCA", "Hotdeck", "Mode"))

如果您需要更多翻译或有其他问题,请告诉我。

英文:

I´m creating a plot using the facet_grid function of ggplot2 where every panel consists of a scatterplot with connected lines of 5 variables.
My data can be found here.
I used this code from the referred link to create the output below.

library(tidyverse)
library(ggplot2)
p&lt;-2.5    #PointSize
lw&lt;-0.7  #Lineweight

plot_data %&gt;%
  mutate(across(where(is.character),
                ~parse_number(.x, locale = locale(decimal_mark = &quot;,&quot;)))) %&gt;%
  pivot_longer(-c(Ausfallrate, AnzahlJ, AnzahlI),
               names_to = &quot;Type&quot;,
               values_to = &quot;PFC&quot;) %&gt;%
  mutate(Type = factor(Type, levels = c(&quot;PFC_RIMCA&quot;, &quot;PFC_MIMCA&quot;, &quot;PFC_Hot&quot;, &quot;PFC_Mode&quot;))) %&gt;%
  ggplot(aes(x = Ausfallrate, y = PFC,
             group = Type, color = Type,
             shape = Type)) +
  geom_point(size=p) +
  geom_line(linewidth=lw) +
  facet_grid(rows = vars(AnzahlI), cols = vars(AnzahlJ),
             labeller = labeller(
               AnzahlJ = c(`4` = &quot;J=4&quot;, `7` = &quot;J=7&quot;, `10` = &quot;J=10&quot;),
               AnzahlI = c(`100` = &quot;I=100&quot;, `500` = &quot;I=500&quot;, `1000` = &quot;I=1000&quot;)
             )) +
  
  
  scale_color_manual(values = c(&quot;blue&quot;, &quot;black&quot;, &quot;red&quot;, &quot;orange&quot;),
                     labels = c(&quot;RIMCA&quot;, &quot;MIMCA&quot;, &quot;Hotdeck&quot;, &quot;Mode&quot;)) +
  scale_shape_manual(values = c(15, 16, 17,18),
                     labels = c(&quot;RIMCA&quot;, &quot;MIMCA&quot;, &quot;Hotdeck&quot;, &quot;Mode&quot;))

facet_grid,绘图中出现额外的意外NA线。

Somehow there is another NA variable picked up from somewhere. I need to get rid of it but can´t figure out where the problem lies. Can anyone help?

EDIT: Here is plot_data:

dput(plot_data)
structure(list(AnzahlI = c(100L, 100L, 100L, 100L, 100L, 100L, 
100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 500L, 500L, 
500L, 500L, 500L, 500L, 500L, 500L, 500L, 500L, 500L, 500L, 500L, 
500L, 500L, 1000L, 1000L, 1000L, 1000L, 1000L, 1000L, 1000L, 
1000L, 1000L, 1000L, 1000L, 1000L, 1000L, 1000L, 1000L), AnzahlJ = c(4L, 
4L, 4L, 4L, 4L, 7L, 7L, 7L, 7L, 7L, 10L, 10L, 10L, 10L, 10L, 
4L, 4L, 4L, 4L, 4L, 7L, 7L, 7L, 7L, 7L, 10L, 10L, 10L, 10L, 10L, 
4L, 4L, 4L, 4L, 4L, 7L, 7L, 7L, 7L, 7L, 10L, 10L, 10L, 10L, 10L
), Ausfallrate = c(&quot;0,1&quot;, &quot;0,2&quot;, &quot;0,3&quot;, &quot;0,4&quot;, &quot;0,5&quot;, &quot;0,1&quot;, 
&quot;0,2&quot;, &quot;0,3&quot;, &quot;0,4&quot;, &quot;0,5&quot;, &quot;0,1&quot;, &quot;0,2&quot;, &quot;0,3&quot;, &quot;0,4&quot;, &quot;0,5&quot;, 
&quot;0,1&quot;, &quot;0,2&quot;, &quot;0,3&quot;, &quot;0,4&quot;, &quot;0,5&quot;, &quot;0,1&quot;, &quot;0,2&quot;, &quot;0,3&quot;, &quot;0,4&quot;, 
&quot;0,5&quot;, &quot;0,1&quot;, &quot;0,2&quot;, &quot;0,3&quot;, &quot;0,4&quot;, &quot;0,5&quot;, &quot;0,1&quot;, &quot;0,2&quot;, &quot;0,3&quot;, 
&quot;0,4&quot;, &quot;0,5&quot;, &quot;0,1&quot;, &quot;0,2&quot;, &quot;0,3&quot;, &quot;0,4&quot;, &quot;0,5&quot;, &quot;0,1&quot;, &quot;0,2&quot;, 
&quot;0,3&quot;, &quot;0,4&quot;, &quot;0,5&quot;), PFC_RIMCA = c(&quot;0,563333333&quot;, &quot;0,564166667&quot;, 
&quot;0,555833333&quot;, &quot;0,556875&quot;, &quot;0,582833333&quot;, &quot;0,556666667&quot;, &quot;0,565&quot;, 
&quot;0,573809524&quot;, &quot;0,565595238&quot;, &quot;0,581619048&quot;, &quot;0,559333333&quot;, &quot;0,556833333&quot;, 
&quot;0,563888889&quot;, &quot;0,571583333&quot;, &quot;0,577266667&quot;, &quot;0,5465&quot;, &quot;0,541916667&quot;, 
&quot;0,575666667&quot;, &quot;0,55675&quot;, &quot;0,538966667&quot;, &quot;0,540857143&quot;, &quot;0,539095238&quot;, 
&quot;0,547333333&quot;, &quot;0,545833333&quot;, &quot;0,548038095&quot;, &quot;0,530466667&quot;, &quot;0,535966667&quot;, 
&quot;0,543666667&quot;, &quot;0,5506&quot;, &quot;0,553253333&quot;, &quot;0,536833333&quot;, &quot;0,549166667&quot;, 
&quot;0,5505&quot;, &quot;0,554958333&quot;, &quot;0,544383333&quot;, &quot;0,53152381&quot;, &quot;0,537309524&quot;, 
&quot;0,543063492&quot;, &quot;0,54272619&quot;, &quot;0,555428571&quot;, &quot;0,5348&quot;, &quot;0,53255&quot;, 
&quot;0,535955556&quot;, &quot;0,540391667&quot;, &quot;0,545173333&quot;), PFC_MIMCA = c(&quot;0,630833333&quot;, 
&quot;0,60375&quot;, &quot;0,592222222&quot;, &quot;0,599375&quot;, &quot;0,6145&quot;, &quot;0,593809524&quot;, 
&quot;0,608809524&quot;, &quot;0,614920635&quot;, &quot;0,603690476&quot;, &quot;0,620666667&quot;, &quot;0,608666667&quot;, 
&quot;0,613&quot;, &quot;0,613444444&quot;, &quot;0,608416667&quot;, &quot;0,612133333&quot;, &quot;0,607333333&quot;, 
&quot;0,599833333&quot;, &quot;0,632055556&quot;, &quot;0,610416667&quot;, &quot;0,591366667&quot;, &quot;0,599619048&quot;, 
&quot;0,597142857&quot;, &quot;0,601111111&quot;, &quot;0,59797619&quot;, &quot;0,600971429&quot;, &quot;0,597133333&quot;, 
&quot;0,599466667&quot;, &quot;0,597577778&quot;, &quot;0,5998&quot;, &quot;0,606866667&quot;, &quot;0,588666667&quot;, 
&quot;0,606666667&quot;, &quot;0,609277778&quot;, &quot;0,613791667&quot;, &quot;0,60575&quot;, &quot;0,59&quot;, 
&quot;0,59002381&quot;, &quot;0,600587302&quot;, &quot;0,597916667&quot;, &quot;0,606533333&quot;, &quot;0,597566667&quot;, 
&quot;0,596083333&quot;, &quot;0,596155556&quot;, &quot;0,596308333&quot;, &quot;0,60184&quot;), PFC_Hot = c(&quot;0,643333333&quot;, 
&quot;0,640416667&quot;, &quot;0,622222222&quot;, &quot;0,631875&quot;, &quot;0,653&quot;, &quot;0,633809524&quot;, 
&quot;0,634761905&quot;, &quot;0,652539683&quot;, &quot;0,629642857&quot;, &quot;0,643333333&quot;, &quot;0,635666667&quot;, 
&quot;0,644666667&quot;, &quot;0,633666667&quot;, &quot;0,636416667&quot;, &quot;0,641266667&quot;, &quot;0,6455&quot;, 
&quot;0,634583333&quot;, &quot;0,659277778&quot;, &quot;0,6425&quot;, &quot;0,620333333&quot;, &quot;0,636285714&quot;, 
&quot;0,642047619&quot;, &quot;0,646253968&quot;, &quot;0,64002381&quot;, &quot;0,630419048&quot;, &quot;0,641133333&quot;, 
&quot;0,634033333&quot;, &quot;0,64&quot;, &quot;0,63995&quot;, &quot;0,647226667&quot;, &quot;0,646333333&quot;, 
&quot;0,645666667&quot;, &quot;0,645722222&quot;, &quot;0,643875&quot;, &quot;0,62955&quot;, &quot;0,633&quot;, 
&quot;0,639547619&quot;, &quot;0,64615873&quot;, &quot;0,637964286&quot;, &quot;0,643704762&quot;, &quot;0,640633333&quot;, 
&quot;0,640116667&quot;, &quot;0,6407&quot;, &quot;0,64115&quot;, &quot;0,64124&quot;), PFC_Mode = c(&quot;0,581666667&quot;, 
&quot;0,56625&quot;, &quot;0,563333333&quot;, &quot;0,568125&quot;, &quot;0,589&quot;, &quot;0,55952381&quot;, 
&quot;0,572142857&quot;, &quot;0,586507937&quot;, &quot;0,578214286&quot;, &quot;0,592380952&quot;, &quot;0,574333333&quot;, 
&quot;0,584333333&quot;, &quot;0,584333333&quot;, &quot;0,5835&quot;, &quot;0,579066667&quot;, &quot;0,571&quot;, 
&quot;0,56275&quot;, &quot;0,5965&quot;, &quot;0,577625&quot;, &quot;0,548633333&quot;, &quot;0,572285714&quot;, 
&quot;0,572&quot;, &quot;0,573365079&quot;, &quot;0,5665&quot;, &quot;0,566304762&quot;, &quot;0,577333333&quot;, 
&quot;0,5648&quot;, &quot;0,5724&quot;, &quot;0,575033333&quot;, &quot;0,575933333&quot;, &quot;0,562333333&quot;, 
&quot;0,571583333&quot;, &quot;0,573361111&quot;, &quot;0,570458333&quot;, &quot;0,5612&quot;, &quot;0,558190476&quot;, 
&quot;0,564785714&quot;, &quot;0,571412698&quot;, &quot;0,5665&quot;, &quot;0,575847619&quot;, &quot;0,57&quot;, 
&quot;0,570233333&quot;, &quot;0,567988889&quot;, &quot;0,57115&quot;, &quot;0,57254&quot;), cc_diff_RIMCA = c(&quot;0,016528237&quot;, 
&quot;0,025934539&quot;, &quot;0,03559416&quot;, &quot;0,069072125&quot;, &quot;0,087847325&quot;, &quot;0,009451442&quot;, 
&quot;0,02369628&quot;, &quot;0,032719389&quot;, &quot;0,064947858&quot;, &quot;0,091211577&quot;, &quot;0,005946948&quot;, 
&quot;0,013185638&quot;, &quot;0,036080129&quot;, &quot;0,06025874&quot;, &quot;0,066632947&quot;, &quot;0,012914223&quot;, 
&quot;0,023702775&quot;, &quot;0,039843616&quot;, &quot;0,06040054&quot;, &quot;0,063749967&quot;, &quot;0,007026154&quot;, 
&quot;0,021995608&quot;, &quot;0,033452241&quot;, &quot;0,060813706&quot;, &quot;0,085071365&quot;, &quot;0,004470022&quot;, 
&quot;0,017360614&quot;, &quot;0,036334928&quot;, &quot;0,0491142&quot;, &quot;0,073710424&quot;, &quot;0,012166285&quot;, 
&quot;0,026675009&quot;, &quot;0,042073547&quot;, &quot;0,041188709&quot;, &quot;0,072048482&quot;, &quot;0,006813497&quot;, 
&quot;0,023293807&quot;, &quot;0,035572192&quot;, &quot;0,054865359&quot;, &quot;0,079219827&quot;, &quot;0,004764175&quot;, 
&quot;0,015975069&quot;, &quot;0,039399602&quot;, &quot;0,059756861&quot;, &quot;0,086189557&quot;), 
cc_diff_MIMCA = c(&quot;0,008545089&quot;, &quot;0,015279067&quot;, &quot;0,018147053&quot;, 
&quot;0,026763839&quot;, &quot;0,028825688&quot;, &quot;0,006160887&quot;, &quot;0,009635058&quot;, 
&quot;0,016014299&quot;, &quot;0,014905519&quot;, &quot;0,024981289&quot;, &quot;0,007759461&quot;, 
&quot;0,009700148&quot;, &quot;0,0123199&quot;, &quot;0,014196057&quot;, &quot;0,022884173&quot;, 
&quot;0,008541805&quot;, &quot;0,011543938&quot;, &quot;0,015185342&quot;, &quot;0,020074829&quot;, 
&quot;0,016130154&quot;, &quot;0,009285704&quot;, &quot;0,012433618&quot;, &quot;0,01984776&quot;, 
&quot;0,02179954&quot;, &quot;0,024935798&quot;, &quot;0,011110233&quot;, &quot;0,017876001&quot;, 
&quot;0,021299103&quot;, &quot;0,029435958&quot;, &quot;0,032467425&quot;, &quot;0,005384345&quot;, 
&quot;0,015501999&quot;, &quot;0,016597473&quot;, &quot;0,023077679&quot;, &quot;0,028536851&quot;, 
&quot;0,009496166&quot;, &quot;0,015289784&quot;, &quot;0,020632933&quot;, &quot;0,024426068&quot;, 
&quot;0,028279466&quot;, &quot;0,011199897&quot;, &quot;0,02244712&quot;, &quot;0,025014936&quot;, 
&quot;0,030655453&quot;, &quot;0,032333572&quot;), cc_diff_Hot = c(&quot;0,010765516&quot;, 
&quot;0,019730651&quot;, &quot;0,024960187&quot;, &quot;0,02336593&quot;, &quot;0,025591872&quot;, 
&quot;0,014072627&quot;, &quot;0,016426783&quot;, &quot;0,022864375&quot;, &quot;0,019199046&quot;, 
&quot;0,017847501&quot;, &quot;0,014007075&quot;, &quot;0,022555706&quot;, &quot;0,021563287&quot;, 
&quot;0,017163961&quot;, &quot;0,017292924&quot;, &quot;0,018236147&quot;, &quot;0,02932634&quot;, 
&quot;0,042508327&quot;, &quot;0,036968754&quot;, &quot;0,03721095&quot;, &quot;0,022123463&quot;, 
&quot;0,036240424&quot;, &quot;0,049441243&quot;, &quot;0,056345175&quot;, &quot;0,049245249&quot;, 
&quot;0,021739254&quot;, &quot;0,037769794&quot;, &quot;0,050983614&quot;, &quot;0,05878995&quot;, 
&quot;0,06252088&quot;, &quot;0,018921326&quot;, &quot;0,033435539&quot;, &quot;0,045997301&quot;, 
&quot;0,04875047&quot;, &quot;0,057026373&quot;, &quot;0,021594149&quot;, &quot;0,039504864&quot;, 
&quot;0,054880791&quot;, &quot;0,062405997&quot;, &quot;0,069283829&quot;, &quot;0,023218275&quot;, 
&quot;0,042964265&quot;, &quot;0,058280896&quot;, &quot;0,070107116&quot;, &quot;0,075758943&quot;
), cc_diff_Mode = c(&quot;0,010885531&quot;, &quot;0,021434552&quot;, &quot;0,024906295&quot;, 
&quot;0,026682435&quot;, &quot;0,04532522&quot;, &quot;0,013263649&quot;, &quot;0,01848612&quot;, 
&quot;0,031707821&quot;, &quot;0,033248562&quot;, &quot;0,042412284&quot;, &quot;0,013806178&quot;, 
&quot;0,023211061&quot;, &quot;0,031983334&quot;, &quot;0,037675067&quot;, &quot;0,04441961&quot;, 
&quot;0,014890828&quot;, &quot;0,025749512&quot;, &quot;0,038202813&quot;, &quot;0,043842752&quot;, 
&quot;0,044076137&quot;, &quot;0,018073945&quot;, &quot;0,030476078&quot;, &quot;0,047319344&quot;, 
&quot;0,057618151&quot;, &quot;0,060244362&quot;, &quot;0,018545751&quot;, &quot;0,033355492&quot;, 
&quot;0,050042107&quot;, &quot;0,061065109&quot;, &quot;0,073070392&quot;, &quot;0,015159231&quot;, 
&quot;0,02912201&quot;, &quot;0,039967542&quot;, &quot;0,048058626&quot;, &quot;0,057664487&quot;, 
&quot;0,017693066&quot;, &quot;0,034957187&quot;, &quot;0,049530579&quot;, &quot;0,061383424&quot;, 
&quot;0,072409922&quot;, &quot;0,019670343&quot;, &quot;0,037953013&quot;, &quot;0,053566002&quot;, 
&quot;0,066653289&quot;, &quot;0,079602554&quot;)), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, 
&quot;data.frame&quot;), row.names = c(NA, -45L))

答案1

得分: 2

If you compare the output of the code snippets, you will see that the variables that the columns in the original data which start with cc_diff get mapped to NA, since this is not one of your factor levels for Type. I would suggest adding these to the factor type, but from your code, I gather that you may want to ignore these data altogether. So the next steps depend on what you are doing, but this addresses the NA lines in the graphs.

英文:

If you compare the output of

plot_data %&gt;%
mutate(across(where(is.character),
~parse_number(.x, locale = locale(decimal_mark = &quot;,&quot;)))) %&gt;%
pivot_longer(-c(Ausfallrate, AnzahlJ, AnzahlI),
names_to = &quot;Type&quot;,
values_to = &quot;PFC&quot;) %&gt;% View()

with

plot_data %&gt;%
mutate(across(where(is.character),
~parse_number(.x, locale = locale(decimal_mark = &quot;,&quot;)))) %&gt;%
pivot_longer(-c(Ausfallrate, AnzahlJ, AnzahlI),
names_to = &quot;Type&quot;,
values_to = &quot;PFC&quot;) %&gt;% 
mutate(Type = factor(Type, levels = c(&quot;PFC_RIMCA&quot;, &quot;PFC_MIMCA&quot;, &quot;PFC_Hot&quot;, &quot;PFC_Mode&quot;))) %&gt;% View()

You will see that the variables that the columns in the original data which start with cc_diff get mapped to NA, since this is not one of your factor levels for Type. I would suggest adding these to the factor type, but from your code I gather that you may want to ignore these data altogether. So the next steps depend on what you are doing, but this addresses the NA lines in the graphs.

答案2

得分: 2

你可以在绘制数据之前使用 filter(!is.na(Type)) 进行筛选。

英文:

You could filter(!is.na(Type)) before ggplotting the data.
facet_grid,绘图中出现额外的意外NA线。

huangapple
  • 本文由 发表于 2023年5月22日 03:28:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301586.html
匿名

发表评论

匿名网友

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

确定