cppiscute
cppiscute

Reputation: 737

How to manage x axis ticks when handling dynamic x asix through shiny (inside ggplot)

Following is my plot function,

I have used implemented code here and not a reproducible one, because I just want to know the concept of handling things here.

print(ggplot(subset(gg1,!is.na(var)), aes_string(x = "Day", y = var, group = "Mi")) +
                geom_point(aes(color = factor(Mi)), size = 5, alpha = 0.7) + 
                #scale_x_continuous(breaks=pretty_breaks(n=10)) + #geom_smooth(stat= "smooth" , alpha = I(0.4), method="loess",color="grey", formula = y ~ x)  
                scale_color_manual("Mesocosm", values = c('#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF', '#808080', '#800000' , '#008000', '#008080')) +
                scale_y_continuous(breaks=pretty_breaks(n=10)) +
                theme_bw() +
                geom_line(data = (ggl), size = 0.5) +
                theme (legend.position = "right", legend.title=element_text(size=14),
                       panel.border = element_rect(colour = "black"),strip.background = element_rect(fill="#CCCCFF"), 
                       strip.text.x = element_text(size=14, face="bold"),axis.text.y = element_text(colour="grey20",size=13,face="bold"),
                       axis.text.x = element_text(colour="grey20",size=13,face="bold"),
                       axis.title.x = element_text(colour="grey20",size=20,face="bold"),
                       axis.title.y = element_text(colour="grey20",size=20,face="bold")) +
                xlim(input$slider[1],input$slider[2]) +
                scale_x_continuous(breaks=pretty_breaks(n=10)) )

I want to split the x asix ticks to accomodate more ticks on the x axis. this I can do using scale_x_continuous as shown in the above example. The result is fine and I get the ticks as I wanted.

What is ticks? A similar question can be found here: [Pretty Breaks][1]

But in the above implementation the dynamic x axis fails to do its operation,

Dynamic x axis: change the slider bar points to make the x axis to adjust automatically.

Next: if I reverse the order of last two lines like

scale_x_continuous(breaks=pretty_breaks(n=10)) + xlim(input$slider[1],input$slider[2]) )

Then scale_x _continuous doesn't work saying "Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale." (Which eliminates the having many ticks as i intend of having ).

How can I implement both in this case. [Want to have dynamic x axis and also want to overwrite the predefined ticks and have more ticks.]

The overview can be seen in this pic. ![enter image description here][2]

The pic is showing even though the slider bar values are changed , the x axis is not adjusting that is because as I said the order of scale_x_continuous and xlim.

How Can I make both work?

Upvotes: 0

Views: 1498

Answers (1)

GregF
GregF

Reputation: 1392

I think limits in the scale_x_continous() function is what you want.

Replace:

xlim(input$slider[1],input$slider[2]) +
                scale_x_continuous(breaks=pretty_breaks(n=10)) )

With:

scale_x_continuous(breaks=pretty_breaks(n=10), limits=c(input$slider[1],input$slider[2])) )

Upvotes: 1

Related Questions