Automatic scale with ggplot



I've made an application shiny and I want to plot the graph with ggplo2.

My question is :
when I use the script below in my shiny application, I obtain the GRAPH A (see below) that's wrong!
when I use the same script directly in rstudio I obtained the GRAPH B, that is good!
WHY don't I the same graph? Have you an idea?

Thanks in advance to your help

my script in shiny

    output$graph_perf<- renderPlot({
      perf.project.tdy<-perf.project %>%
      p<-factor(perf.project.tdy$pas, levels = (unique(perf.project.tdy$pas)), ordered=TRUE)
      ggplot(perf.project.tdy,aes(x=p , y=perf.project.tdy$perf,group = types)) + 
        geom_bar(stat="identity",position="stack",fill="steelblue") + 
        geom_text(aes(label=perf), vjust=1.6, color="white", size=3.5)+
        theme(panel.background = element_blank(),
              panel.grid.minor = element_blank(),
              axis.ticks  = element_blank(),
              axis.line   = element_line(colour=NA),
              axis.line.x = element_line(colour="grey80"))





Hello, I would like know if my problem it's clear.
Thanks in advance



Could you please turn this into a self-contained reprex (short for reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.


If you've never heard of a reprex before, you might want to start by reading the help page. The reprex dos and don'ts are also useful.

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "")

For pointers specific to the community site, check out the reprex FAQ, and the part on shiny reprex. Thanks!



Thanks to your reply.

So I have some problem with reprex packages :

Preparing reprex as .R file:
  * test_reprex.R
Rendering reprex...
Writing reprex markdown:
Error in file(con, "w") : cannot open the connection
In addition: Warning messages:
1: In readLines(path) : incomplete final line found on '1_10.R'
2: In file(con, "w") :
  cannot open file '': Invalid argument


I realy need to your help, I have to submit a first draft of my interface on the next week.



About reprex, please, follow the beginner guide about reprex from the FAQ. Try to identify your issue and build a small example.
also this webinar is recent and can help you build a reprex

We can help you if you can help us help you, and only in the scope of our Homework Policy.

1 Like


Giving folks a reprex will certainly help. A few thoughts without knowing anything about your data:

  1. You should drop the print(perf.project.tdy) line of code, as that appears to just be a leftover from your original attempts to check your work. It might be causing problems in the Shiny app.
  2. Why have you elected to create an object p outside of the primary dataframe? My suspicion is that separating the X and Y variables into distinct objects (i.e., a dataframe and a vector) is causing mischief here. I think you'd be better served using the forcats package to convert perf.project.tdy$pas into a factor and releveling it, rather than creating a separate object.
  3. Consider chaining all your code together into a single pipeline using %>%. It would help to make your code more readable and leaner.
  4. The ggplot2 documentation recommends using geom_col with stat="identity", while geom_bar is more suitable for stat="count". You also don't really need the position="stack" parameter here, as you're not attempting to create stacked bars in any case.


Hello, no problem is my homework.

I'll try to build a reprex. Let's start!



Hello, thanks to your reply.

2- I used a variable p as factor because I have a message error if I don't use it.
3 - I'm not sure to do understand. How can I use the pipeline %>% out of my output.
4 - You are right, it's not necessary to use position="stack"

I found my issue.
In reactive fiunction I calculate perf.project.tdy$perfand I add "%" If I leave the"%" the value is not a numuric. If I take off "%" it's OK.

How can I leave the ratio value with the symbol "%" to plot my function?
Thanks in advance

PS : I tried to use reprex. I installed the package reprex and there is a error message : reprex is unknown function



The annotations are created by geom_text(), so you want to format the numbers in that step. One way to do that is something like:

geom_text(aes(label=paste(perf, "%")), vjust=1.6, color="white", size=3.5)

For more flexible number formatting, see the scales package

If you’d like help getting the reprex package installed and working, please open a separate topic for that question. Note that since this was about a problem in a shiny app, formatting your example with reprex would not have worked. Shiny reproducible examples are a bit more complicated to create. See this FAQ for help: Shiny debugging and reprex guide.

However, the same basic principles apply: if people can’t run your code, then in most cases they can’t figure out your problem. Here, it was impossible for anybody else to find the problem since it turned out to be in a part of the shiny app code that you didn’t include in your post. It’s not very efficient for you to have helpers stuck just guessing what might be wrong due to incomplete information, and it’s not terribly respectful of your helpers’ time, either.

1 Like