rahul67
1
I m trying to make this figure but getting an error. can some body help me what is my mistake.

below is my code
library("ggplot2")
library(plotly)
x.values <- seq(0, 20, 1)
break.values <- seq(0, 20, 2)
zeros11 <- rep(0, 11)
y1 <- dpois(x.values, 9)
y2 <- dpois(x.values, 5)
df2 <- data.frame(x.values, y1)
y <- ggplot(df2, aes(x=x.values, y=y1))+
geom_bar(stat="identity", width = 0.5)+labs(x = "x-Werte", y = "Werte Poissonverteilungen")+
scale_x_continuous(breaks=break.values, labels=break.values)
Edited. This should work.
As mentioned by @FJCC below, you didn't call y
. I imagine that you wanted to put it in to a plotly
object which is why you have called the library.
library(tidyverse)
library(plotly)
x.values <- seq(0, 20, 1)
break.values <- seq(0, 20, 2)
y1 <- dpois(x.values, 9)
y2 <- dpois(x.values, 5)
y3 <- dpois(x.values, 1)
df3 <- data.frame(x.values, y1, y2, y3) %>%
pivot_longer(-x.values, names_to = "lambda", values_to = "values") %>%
mutate(lambda = case_when(lambda == "y1" ~ "lambda = 5",
lambda == 'y2' ~ "lambda = 9",
lambda == 'y3' ~ "lambda = 1",
TRUE ~ NA_character_))
g <- ggplot(df3, aes(x.values, y = values, fill = lambda)) +
geom_col(position = "dodge") +
labs(x = "x-Werte", y = "Werte Poissonverteilungen", fill = NULL) +
scale_x_continuous(breaks=break.values, labels=break.values)
ggplotly(g)
FJCC
3
The posted code works to make the plot when lambda = 9 if I append a final call of y to print out the graph
library("ggplot2")
library(plotly)
#>
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#>
#> last_plot
#> The following object is masked from 'package:stats':
#>
#> filter
#> The following object is masked from 'package:graphics':
#>
#> layout
x.values <- seq(0, 20, 1)
break.values <- seq(0, 20, 2)
zeros11 <- rep(0, 11)
y1 <- dpois(x.values, 9)
y2 <- dpois(x.values, 5)
df2 <- data.frame(x.values, y1)
y <- ggplot(df2, aes(x=x.values, y=y1))+
geom_bar(stat="identity", width = 0.5)+labs(x = "x-Werte", y = "Werte Poissonverteilungen")+
scale_x_continuous(breaks=break.values, labels=break.values)
y

Created on 2020-02-13 by the reprex package (v0.2.1)
1 Like
rahul67
4
trying to use pivot_longer but showing error. Is there any package for pivot_longer?
pivot_longer
is in tidyr
which is part of the tidyverse
.
rahul67
6
using pivot_longer with tidyverse package but showing error.
You might need to update tidyr
as pivot_longer
is fairly new.
rahul67
8
It is not working after updating the packages. It is still showing error.
Maybe it needs a restart?
Anyway, try this. It uses gather
instead:
library(tidyverse)
library(plotly)
x.values <- seq(0, 20, 1)
break.values <- seq(0, 20, 2)
y1 <- dpois(x.values, 9)
y2 <- dpois(x.values, 5)
y3 <- dpois(x.values, 1)
data.frame(x.values, y1, y2, y3) %>%
gather(-x.values, key = "lambda", value = "values") %>%
mutate(lambda = case_when(lambda == "y1" ~ "lambda = 5",
lambda == 'y2' ~ "lambda = 9",
lambda == 'y3' ~ "lambda = 1",
TRUE ~ NA_character_))
g <- ggplot(df3, aes(x.values, y = values, fill = lambda)) +
geom_col(position = "dodge") +
labs(x = "x-Werte", y = "Werte Poissonverteilungen", fill = NULL) +
scale_x_continuous(breaks=break.values, labels=break.values)
ggplotly(g)
rahul67
10
Thankyou for your help. Really appreciate it.
Williammmm, now we'll never know what the pivot_longer error was !

2 Likes
system
Closed
12
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.